Kevin K
Kevin K

Reputation: 2227

My partial is not where rails expects it to be (nested partials)

I have a model Submissions which has many Performers. I have a partial for showing an individual submissions (app/views/submissions/_submission.html.erb):

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

and a partial for showing performers (app/views/performers/_performer.html.erb):

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

This works fine from (app/views/submissions/show.html.erb):

<%= render @submission %>

But I want to use this from a different namespace too (app/views/curator/submissions/show.html.erb). But I get this error:

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"

I can fix this by changing the render to this:

<%= render 'submissions/submission' %>

But, then the nested partial fails:

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"

This doesn't work:

<%= render 'performers/performer' %>

because of the div_for:

undefined method `model_name' for NilClass:Class

Any ideas how I should do this?

UPDATE: In addition to the change that iouri suggested, I added this to my performer partial:

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

Upvotes: 1

Views: 254

Answers (1)

iouri
iouri

Reputation: 2929

You can move them into a separate folder under views, called partials for examples, then reference them from there in both places, you will need to pass your object to the partial:

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

Upvotes: 1

Related Questions