4

我正在使用带有标准导轨形式和 Cocoon gem 的 Rails 3。我想使用 gem 提供的 'render_options' 通过 'link_to_add_association' 将变量发送到部分,但我无法使其工作。部分视图渲染正常,coocon 工作正常(可以添加和删除项目),但变量除外。尝试在视图中打印变量表示未定义。这是我的代码:

_form.html.erb(这是从 new.html.erb 调用的部分)

<%= link_to_add_association raw('Nuevo con empleado existente'), f, parte_diario_item_indirectos, :render_options => {:locals => {:foo => 'bar'}}%>

_parte_diario_item_indirecto_fields.html.erb

<%= foo %>

做这样的事情会导致“未定义的'foo'错误”。

我也试过:

<%= locals[:foo] %>

结果相同。在“render_options”周围使用方括号/大括号也不起作用。

4

1 回答 1

3

我有一个非常相似的问题,直到我意识到在我调用的正上方render的块中也调用了fields_forlink_to_add_association

这是 Cocoon 提供的示例,我的本地人添加到link_to_add_association

= simple_form_for @project do |f|
  = f.input :name
  = f.input :description
  %h3 Tasks
  #tasks
    = f.simple_fields_for :tasks do |task|
      = render 'task_fields', :f => task
    .links
      = link_to_add_association 'add task', f, :tasks, :render_options => { :locals => { :foo => 'bar'} }
  = f.submit

我没有将本地人传递给第一次render调用,所以实际的未定义错误来自错误render,而不是来自link_to_add_association

于 2015-06-16T21:03:40.740 回答