我有一个帐户和场所表,其中一个帐户可以有许多场所。
我在帐户索引页面上将所有帐户显示为部分帐户,并希望每个帐户都包含与其链接的场所的名称。
这是我所拥有的:
帐户部分
<%= link_to free_account do %>
<div class="account_partial">
<span class="account_header"><%= free_account.name %></span> - <span class="free_account_highlight">(<%= free_account.role %>)</span><br>
<%= render :partial => 'venues/account_venue', :collection => @account.venues %>
</div>
<% end %>
account_venue 部分
<%= venue.name %>
我收到此错误:
帐户中的 NoMethodError#index
nil:NilClass 的未定义方法“场地”
提取的源代码(第 12 行附近):
12: <%= 渲染 :partial => 'venues/account_venue', :collection => @account.venues %>
任何帮助将非常感激!
编辑
account_controller
class AccountsController < ApplicationController
load_and_authorize_resource
def index
@accounts = Account.all(:include => :venues)
end
end
帐户 index.html.erb
<div id="narrow_container">
<div class="free_accounts_container">
<h2 class="show_orange">Free accounts</h3>
<%= render :partial => 'free_account', :collection => @accounts %>
</div>
<div class="premium_accounts_container">
<h2 class="show_orange">Premium accounts</h3>
<%= render :partial => 'premium_account', :collection => @accounts %><br><br>
</div>
<div class="clearall"></div>
<div class="button">
<%= link_to 'add account', new_account_path %>
</div>
</div>
_free_account.html.erb
<%= link_to free_account do %>
<% if free_account.role == "free" %>
<div class="account_partial">
<span class="account_header"><%= free_account.name %></span> - <span class="free_account_highlight">(<%= free_account.role %>)</span><br>
<div class="owner_details">
<span class="pre_account_highlight">Owners username:</span><span class="account_highlight"><%= free_account.user.username %></span>
<span class="pre_account_highlight">Owners e-mail:</span><span class="account_highlight"><%= free_account.user.email %></span>
</div>
<div class="account_details">
</div>
<%= render :partial => 'venues/account_venue', :collection => @account.venues %>
</div>
<% else %>
<% end %>
<% end %>
更新
如果我将部分调用更改为:
<%= render :partial => 'venues/account_venue', :collection => @accounts %>
和 account_venue 部分只是读取“测试”它加载没有错误但显示单词 test 4 次(有 4 个帐户记录)如果我添加一个新帐户记录它显示单词 test 5 次。