Reputation: 3462
I am having trouble calling a method in a partial. The method is defined in the Isbn model. Here's the set up:
Contracts
has_many :isbns, :through => :istcs
Isbns
belong_to :contracts
The Isbn show view successfully displays the result of roy_first
, an instance variable that's defined in the method:
<% @isbn.roy_first.each do |hash| %>
<% hash.each do |channel, roy| %>
<%= "#{channel} 1st: £#{roy}" %>
<% end %><% end %>
As I say, roy_first
is an instance variable defined in Isbn.rb:
def sales_lookup_complete
# a bunch of code...
@roy_first << {channel_name => ra = df*(u/100)}
end
Here's the isbns_controller show, if it's relevant:
@isbn.sales_lookup_complete
But when I try to call the same instance variable in an _isbn partial, I get an undefined method error. I get this error when I use a local or an instance variable.
My question: How do I call the same in a partial which is accessed from contracts > show.html.erb?
The partial is called from the contracts show page like this:
<%= render @isbns %>
Many thanks in advance.
Here is some more code as requested:
The contracts controller show method:
def show
@title = (@contract.contract_name + "'s contract")
@istcs = @contract.istcs.paginate(:page => params[:page])
@isbns = @contract.isbns.paginate(:page => params[:page])
end
The isbns controller show method:
def show
@isbn = Isbn.find(params[:id])
@title = @isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix
@sales = @isbn.sales.paginate(:page => params[:page] || 1)
@isbn.sales_lookup_complete
end
The contract show view:
<h4>ISBNs <span class="add"><%= link_to "Create ISBN", new_isbn_path %></span></h4>
<% unless @contract.isbns.empty? %>
<table summary="Contract ISBNs">
<%= render @isbns %>
</table>
<% end %>
The isbn show view:
<table>
<tr><h5>Royalties for all sales</h5>
<tr><% @isbn.roy_first.each do |hash| %>
<td><% hash.each do |channel, roy| %>
<%= "#{channel} 1st: £#{roy}" %>
</td><% end %><% end %>
</tr>
</table>
This is the bit that causes the error, in the partial called _isbn, and which is in the isbn views area, but called from the contracts show view:
Id: <%= isbn.id%>
<tr><% @isbn.roy_first.each do |hash| %>
<td><% hash.each do |channel, roy| %>
<%= "#{channel} 1st: £#{roy}" %>
</td><% end %><% end %>
</tr>
The isbn.id bit works fine. The code in tags gives the following error:
NoMethodError in Contracts#show
Showing /app/views/isbns/_isbn.html.erb where line #6 raised:
undefined method `roy_first' for nil:NilClass
Extracted source (around line #6):
3: <span class="content"><%= link_to isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix, edit_isbn_path(isbn.id) %> </span>
4: <span class="timestamp">
5: Id: <%= isbn.id%>
6: <tr><% @isbn.roy_first.each do |hash| %>
7: <td><% hash.each do |channel, roy| %>
8: <%= "#{channel} 1st: £#{roy}" %>
9: </td><% end %><% end %>
And making it a local variable gives:
NoMethodError in Contracts#show
Showing /app/views/isbns/_isbn.html.erb where line #6 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #6):
3: <span class="content"><%= link_to isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix, edit_isbn_path(isbn.id) %> </span>
4: <span class="timestamp">
5: Id: <%= isbn.id%>
6: <tr><% isbn.roy_first.each do |hash| %>
7: <td><% hash.each do |channel, roy| %>
8: <%= "#{channel} 1st: £#{roy}" %>
9: </td><% end %><% end %>
Trace of template inclusion: app/views/contracts/show.html.erb
Upvotes: 0
Views: 727
Reputation: 17803
I dont know if you are trying to do this, but try this:
contracts_controller - show
def show
@title = (@contract.contract_name + "'s contract")
@istcs = @contract.istcs.paginate(:page => params[:page])
@isbns = @contract.isbns.paginate(:page => params[:page])
end
contracts - show view
<h4>ISBNs <span class="add"><%= link_to "Create ISBN", new_isbn_path %></span></h4>
<% unless @isbns.empty? %>
<table summary="Contract ISBNs">
<% @isbns.each do |isbn| %>
<% isbn.sales_lookup_complete %>
<%= render :partial => "isbn", :locals => {:isbn => isbn} %>
<% end %>
</table>
<% end %>
and _isbn.html.erb
<tr>
<% isbn.roy_first.each do |hash| %>
<td>
<% hash.each do |channel, roy| %>
<%= "#{channel} 1st: £#{roy}" %>
<% end %>
</td>
<% end %>
</tr>
Upvotes: 2
Reputation: 6021
In the _isbn.html.erb
partial:
<% isbn.roy_first.each do |hash| %>
<% hash.each do |channel, roy| %>
<%= "#{channel} 1st: £#{roy}" %>
<% end %>
<% end %>
this should definitely work.
The error occurred while evaluating nil.each
this error mean that roy_first
method returns nil
instead of expected hash
.
Upvotes: 0