在从另一个模型调用的部分中调用模型方法

时间:2011-06-28 15:20:03

标签: ruby-on-rails

我在部分调用方法时遇到问题。该方法在Isbn模型中定义。这是设置:

合同

has_many :isbns, :through => :istcs

的ISBN

belong_to :contracts

Isbn show视图成功显示roy_first的结果,这是在方法中定义的实例变量:

<% @isbn.roy_first.each do |hash| %>
<% hash.each do |channel, roy| %>
<%= "#{channel} 1st: £#{roy}" %> 
<% end %><% end %>

正如我所说,roy_first是Isbn.rb中定义的实例变量:

def sales_lookup_complete
# a bunch of code... 
@roy_first << {channel_name => ra = df*(u/100)}
end 

这是isbns_controller节目,如果它是相关的:

 @isbn.sales_lookup_complete

但是当我尝试在_isbn partial中调用相同的实例变量时,我得到一个未定义的方法错误。当我使用本地或实例变量时,我收到此错误。

我的问题:我如何在从合同中访问的部分中调用相同的&gt; show.html.erb?

从合约展示页面调用部分,如下所示:

<%= render @isbns %>

非常感谢提前。

以下是更多代码:

合约控制人显示方法:

  def show
    @title = (@contract.contract_name + "'s contract")
    @istcs = @contract.istcs.paginate(:page => params[:page])
    @isbns = @contract.isbns.paginate(:page => params[:page])    
  end

isbns控制器显示方法:

  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

合同显示视图:

<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 %>

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>

这是导致错误的位,在部分名为_isbn,位于isbn views区域,但是从合同显示视图调用:

 Id: <%= isbn.id%>
<tr><% @isbn.roy_first.each do |hash| %>
    <td><% hash.each do |channel, roy| %>
        <%= "#{channel} 1st: £#{roy}" %> 
    </td><% end %><% end %>
</tr>

isbn.id位工作正常。标签中的代码会出现以下错误:

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 %>

将其作为局部变量给出:

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

2 个答案:

答案 0 :(得分:2)

我不知道你是否想要这样做,但试试这个:

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

合同 - 显示视图

<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 %>

和_isbn.html.erb

<tr>
  <% isbn.roy_first.each do |hash| %>
    <td>
      <% hash.each do |channel, roy| %>
        <%= "#{channel} 1st: £#{roy}" %> 
      <% end %>
    </td>
  <% end %>
</tr>

答案 1 :(得分:0)

_isbn.html.erb部分:

<% isbn.roy_first.each do |hash| %>
  <% hash.each do |channel, roy| %>
    <%= "#{channel} 1st: £#{roy}" %> 
  <% end %>
<% end %>

这绝对有用。

The error occurred while evaluating nil.each此错误表示roy_first方法返回nil而非预期hash

相关问题