【问题标题】:Rails 4 rendering partials (undefined local variable or method)Rails 4渲染部分(未定义的局部变量或方法)
【发布时间】:2014-03-18 01:17:08
【问题描述】:

我正在尝试将两个局部变量传递给我的局部变量,但是当在局部变量中使用它时,我得到了 # 的未定义局部变量或方法“行”。

渲染

<%= f.fields_for :menu_items do |builder| %>
<%= render partial: 'menu_item_fields', locals: {f: builder, row: f.options[:child_index]} %>
<% end %>`

部分

<a href="#" data-target="item-<%= f.options[:child_index].to_s + "-" + row.to_s %>" class="item-field"><%= f.object.title %></a>

【问题讨论】:

  • 提供错误发生的代码行?
  • 我做到了。其标记为“部分”
  • 我正在使用嵌套属性,需要嵌套项目的构建器
  • 对不起,我只是有一个“duh”的时刻。我的意思是说,因为 :menu_items 是一个集合,你每次都在渲染一个部分rendering a collection may be better suited for this,因为它为集合中的每个项目执行一次。
  • 我会调查的,但我仍然遇到同样的问题

标签: ruby-on-rails ruby-on-rails-4 partials


【解决方案1】:

更改此代码

<a href="#" data-target="item-<%= f.options[:child_index].to_s + "-" + row.to_s %>" class="item-field"><%= f.object.title %></a>

如下:

<a href="#" data-target="item-<%= f.options[:child_index].to_s + '-' + row.to_s %>" class="item-field"><%= f.object.title %></a>

【讨论】:

  • 我试过了,我也试过这样做 但它仍然给出了同样的错误。不过很好。
【解决方案2】:

我认为应该是:

{ f: builder, row: builder.options[:child_index] }

糟糕,我在手机上误读了您的代码。如果您不使用部分调用它,您的代码是否可以正常工作?

【讨论】:

  • 如果是这样的话,我一开始就没有理由通过行。
  • 对,如果你只传递 f 会发生什么?喜欢:parent_form:f。然后在你的部分调用中 parent_form.options[:child_index] ?
  • 同样的事情。它说 parent_form 的未定义。当我有多个局部变量时会出现问题
  • 通过本地人传递多个值应该没问题。您可以通过仅传递 parent_form 并再次调用 parent_form.options[:child_index] 来测试它。它仍然应该失败。
猜你喜欢
  • 2016-11-21
  • 2012-03-04
  • 1970-01-01
  • 2018-04-13
  • 2014-08-15
  • 2018-05-05
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
相关资源
最近更新 更多