1

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

我有一个文件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。没有任何局部变量的原始渲染调用工作正常,即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"

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

4

1 回答 1

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.

于 2014-11-24T20:10:46.927 回答