【问题标题】:Missing template when rendering a collection using local variables使用局部变量呈现集合时缺少模板
【发布时间】:2014-11-24 19:38:33
【问题描述】:

我的 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 中为我的组织显示视图做同样的事情时,我遇到了问题。没有任何局部变量的原始渲染调用工作正常,即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"

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

【问题讨论】:

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


    【解决方案1】:

    如果您尝试在 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 上查找文件。

    【讨论】:

    • 叹息,我知道我试图这样做,但我必须在类别视图或其他东西上完成它。无论如何,现在它可以工作了。谢谢!
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 2013-02-18
    • 2010-10-25
    • 1970-01-01
    • 2011-11-22
    • 2011-05-18
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多