Missing template when rendering a collection using local variables

2.4k Views Asked by At

I have two resources Category and Organization in my Rails 4 application. I'm struggling with rendering partials combined with local variables.

I have a file app/views/categories/_category.html.erb:

<li>link_to category.name, category</li>

In app/views/categories/show.html.erb I can render this partial using

<%= render @categories %>

To pass a local variable to say, bold-face the current category in the list, I can change the method call to

<%= render partial: "category", collection: @categories, as: :category, locals: {active_category: @category} %>

So far so good! The code does what I expect it to do.

But I run into problems when I want to do the same thing for my organizations show-view in the file app/views/organizations/show.html.erb. The original render call without any local variables works fine, i.e. render @categories. The second call however gives me the error

Template is missing

Missing partial organizations/_category, application/_category with {
  :locale=>[:en], 
  :formats=>[:html], 
  :variants=>[], 
  :handlers=>[:erb, :builder, :raw, :ruby, :coffee]
}. 

 Searched in: 
   * "/home/snail/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta4/lib/action_dispatch/templates" 
   * "/home/snail/work/PROJECTNAME/app/views" 
   * "/home/snail/.rvm/gems/ruby-2.1.2/gems/web-console-2.0.0.beta4/app/views" 
   * "/home/snail/.rvm/gems/ruby-2.1.2/gems/devise-3.4.0/app/views"

Why is this, and how do I fix it?

1

There are 1 best solutions below

1
On BEST ANSWER

If you're trying to render the same partial at app/views/categories/_category.html.erb you need to change your render call for your organization's show template.

render partial: "categories/category", collection: @categories, ...

The app/views/organizations/show.html.erb template will otherwise look for a file at app/views/organizations/_category.html.erb.