我的部分不是rails所期望的(嵌套部分)

时间:2012-10-05 15:30:20

标签: ruby-on-rails partial

我有一个模型提交,其中包含许多表演者。我有部分提交个人提交(app / views / submissions / _submission.html.erb):

<div>
  Show stuff relating to @submission
  ...
  <%= render @performers %>
</div>  

和部分表演者(app / views / performers / _performer.html.erb):

<%= div_for performer do %>
  <%= performer.name %>
<% end %>

这可以从(app / views / submissions / show.html.erb)中正常工作:

<%= render @submission %>

但我也希望从不同的命名空间中使用它(app / views / curator / submissions / show.html.erb)。但是我得到了这个错误:

Missing partial curator/submissions/submission with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/Users/ircmullaney/RubyCode/cif/app/views"
  * "/Users/ircmullaney/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/devise-2.1.2/app/views"

我可以通过将渲染更改为此来解决此问题:

<%= render 'submissions/submission' %>

但是,嵌套的部分失败了:

Missing partial curator/performers/performer with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
  * "/Users/ircmullaney/RubyCode/cif/app/views"
  * "/Users/ircmullaney/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/devise-2.1.2/app/views"

这不起作用:

<%= render 'performers/performer' %>

因为div_for:

undefined method `model_name' for NilClass:Class

我应该如何做到这一点?

更新:除了iouri建议的更改,我将此添加到我的表演者部分:

<% @performers.each do |performer| %>
  <%= div_for performer do %>
    <%= performer.name %>
  <% end %>
<% end %>

1 个答案:

答案 0 :(得分:1)

您可以将它们移动到视图下的单独文件夹中,例如,在示例中称为partials,然后在两个地方从那里引用它们,您需要将对象传递给partial:

<%= render 'partials/performer', :locals => {:performer => @performer} %>