render => partial not recognizing method in call

Perhaps it is something basic as a beginner I am. I have the following:

controllers:

main

editions

works

composers

main:

class MainController < ApplicationController

def welcome

@composers = Composer.find(:all).sort_by {|c| [c.last_name, c.first_name]}

end

end

works - editions - composers all have normal scaffold generated methods(CRUD)

Models: - relavent info (hopefully) provided:

class Composer < ActiveRecord::Base

attr_accessible :first_name, :last_name

has_many :works, dependent: :destroy

before_destroy :ensure_not_referenced_by_any_work

def editions

works.map {|work| work.editions}.flatten.uniq

end

end

class Edition < ActiveRecord::Base

validates :publisher_id, :description, :price, :year, presence: true

has_many :environments

has_many :works, through: :environments

belongs_to :publisher

has_many :orders

attr_accessible :descripition, :price, :work_id, :year, :title, :publisher_id

end

class Work < ActiveRecord::Base

attr_accessible :composer_id, :title

belongs_to :composer

has_many :environments

has_many :editions, :through => :environments

has_many :instruments, :through => :environments

end

class Environment < ActiveRecord::Base

attr_accessible :instrument_id, :name, :work_id, :edition_id

belongs_to :work

belongs_to :instrument

belongs_to :edition

end

I want the following partial to be shown in the Main#Welcome view:

partial is located in the composer view folder:

Editions List

<% @composer.editions.map do |edition| %> #line 3 in error below

<li><%= link_to_edition_title(edition)%></li>

<% end %>

The link_to_edition_title(edition)… may not be relavent but here it is in the edition helper module:

def link_to_edition_title(edition)

link_to(edition.nice_title, edition_path(edition))

end

My limited knowledge believes the call to the partial should work and indeed it does if done like this within the Composer#Show view:

<%= render “edition” %>

However, when I make the partial call from the Main#Welcome view like this:

The Main#Welcome view:

<%= render :partial => "composers/edition" %>

It results in this error:

NoMethodError in Main#welcome

Showing /Users/akkdio/railsapps/rails_3/ruby_for_rails/r4r_music/r4r_music1/app/views/composers/_edition.html.erb where line #3 raised:

undefined method `editions’ for nil:NilClass

This is the first linke of the trace:

app/views/composers/_edition.html.erb:3:in `_app_views_composers__edition_html_erb__1574837102347920251_2165688820’

If more is needed please let me know. Appreciate any nudge as to how I would change the partial or somehow get the editions method to be recognized in this call - the idea is to be able to share the partial across the application.

Thanks for your help,

Andrew

Perhaps it is something basic as a beginner I am. I have the following:

There was no need to show all this code, it is easier for us to see
what is going on if you just show the relevant bits

[snip]

I want the following partial to be shown in the Main#Welcome view:

partial is located in the composer view folder:

<h3>Editions List</h3>
<ul>
<% @composer.editions.map do |edition| %> #line 3 in error below
[snip]

My limited knowledge believes the call to the partial should work and indeed
it does if done like this within the Composer#Show view:

<%= render "edition" %>

However, when I make the partial call from the Main#Welcome view like this:
The Main#Welcome view:
<%= render :partial => "composers/edition" %>

It results in this error:

NoMethodError in Main#welcome

Showing
/Users/akkdio/railsapps/rails_3/ruby_for_rails/r4r_music/r4r_music1/app/views/composers/_edition.html.erb
where line #3 raised:

undefined method `editions' for nil:NilClass

That means that @composer is nil, since that is the object on which
'editions' is being called.

Have a look at the Rails Guide on Layouts and Rendering, particularly
the section Using Partials to see how to pass local variables to the
partial.

Also have a look at the guide on debugging to get ideas on how to
debug your code so that you can work out what is going on.

I notice that you have asked for help twice before on this list and
neither time did you reply to the suggestions offered or report
whether you had found a solution. :frowning:

Colin

Hi, Andrew

The error says the @composer variable is not set.

By the code I see, that you are setting @composers variable.

Take a look at the starting guide on rendering in Ruby on Rails as an approach itself is wrong.

Max

Perhaps it is something basic as a beginner I am. I have the following:

There was no need to show all this code, it is easier for us to see

what is going on if you just show the relevant bits

Understand and thanks.

[snip]

I want the following partial to be shown in the Main#Welcome view:

partial is located in the composer view folder:

Editions List

<% @composer.editions.map do |edition| %> #line 3 in error below

[snip]

My limited knowledge believes the call to the partial should work and indeed

it does if done like this within the Composer#Show view:

<%= render “edition” %>

However, when I make the partial call from the Main#Welcome view like this:

The Main#Welcome view:

<%= render :partial => “composers/edition” %>

It results in this error:

NoMethodError in Main#welcome

Showing

/Users/akkdio/railsapps/rails_3/ruby_for_rails/r4r_music/r4r_music1/app/views/composers/_edition.html.erb

where line #3 raised:

undefined method `editions’ for nil:NilClass

That means that @composer is nil, since that is the object on which

‘editions’ is being called.

Have a look at the Rails Guide on Layouts and Rendering, particularly

the section Using Partials to see how to pass local variables to the

partial.

Also have a look at the guide on debugging to get ideas on how to

debug your code so that you can work out what is going on.

Will check this. thanks.

I notice that you have asked for help twice before on this list and

neither time did you reply to the suggestions offered or report

whether you had found a solution. :frowning:

Not an excuse but I was not notified of the someone answering… Sorry. This time I checked back and check “email updates to me” Sorry again - I appreciate all the guidance i can get.

My solution/learning (sort of) I read through the Rails Guide and it was somewhat helpful - lots to learn -

My disconnect however that I was under the impression that the partial because it worked when I used in the composer show method it would work in the Main#Welcome view. Well, this is incorrect because in the Composer#Index view I click on the Composer ID (set up as a link_to on the id:

<%= link_to composer.id, composer_path(composer) %>

This is how the partial gets the composer id in order make the editions partial work and why the editions method returned a Nil when called from the Main#welcome view. It was saying " what composer? I can’t find no stinking Composer" If I do want the editions listed in the Main#Welcome view for each composer then I will have to find a way to send the editions method the composer id it is looking for. Time for more reading.

Thanks all for the nudge.