使用局部变量渲染集合时缺少模板

时间:2014-11-24 19:38:34

标签: ruby-on-rails-4 local-variables missing-template

我的Rails 4应用程序中有两个资源CategoryOrganization。我正在努力将局部变量与局部变量结合起来。

我有一个文件app/views/categories/_category.html.erb

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

app/views/categories/show.html.erb我可以使用

渲染此部分
<%= render @categories %>

要传递一个局部变量来说明,粗体显示列表中的当前类别,我可以将方法调用更改为

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

到目前为止一切顺利!代码完成了我期望它做的事情。

但是当我想在文件app/views/organizations/show.html.erb中为我的组织show-view做同样的事情时,我遇到了问题。没有任何局部变量的原始渲染调用工作正常,即render @categories。然而,第二个电话给我错误

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"

为什么会这样,我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果您尝试在app/views/categories/_category.html.erb处渲染相同的部分内容,则需要更改针对您单位的render模板的show来电。

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

app/views/organizations/show.html.erb模板将在app/views/organizations/_category.html.erb处查找文件。

相关问题