【问题标题】:Rails undefined local variable or method: Rendering partial with join tablesRails 未定义的局部变量或方法:使用连接表渲染部分
【发布时间】:2018-04-13 12:23:28
【问题描述】:

我在公司和其他模型(例如工作、位置、人员)之间有一个联合表

_companies.html.erb

    <% if company.companylogo %>
    <%= link_to (image_tag(company.companylogo.url(:medium), :class => "fw img-responsive", :alt =>"company logo")), company_path(company) %>
    <% else %> 
    <%= link_to (image_tag("http://via.placeholder.com/405x285", :class => "fw img-responsive", :alt =>"company logo")), company_path(company) %>
    <% end %>



   <p <%= link_to truncate(company.name, :length => 20), company_path(company), :class => "" %> </p>

在 job/index.html.erb 中,我能够将上述公司的所有内容部分渲染为

<%= render job.companies if job.companies.present? %>

其中显示了母公司徽标和公司名称。

现在我想通过创建另一个部分将公司名称与徽标分开: _seperatename.html.erb

**<p <%= link_to truncate(company.name, :length => 20), company_path(company), :class => "" %> </p>**

我的问题是,在 job/index.html.erb 中,如何渲染 _seperatename.html.erb ?

我试过

它抛出错误 ActionView::Template::/jobs 处的错误 #

的未定义局部变量或方法“公司”

但是,如果我将 _seperatename.html.erb 中的 ruby​​ 内容替换为正常文本,它会呈现没有错误

【问题讨论】:

  • 有点不清楚你在问什么。但是,我认为你需要render partial: ... DOCS

标签: ruby-on-rails jointable renderpartial


【解决方案1】:

您应该将变量传递给部分:

<%= render partial: 'companies/seperatename', locals: {company: @company} %>

文档:http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2016-11-21
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多