【问题标题】:Cannot send variable to partial using Cocoon (Rails 3)无法使用 Cocoon(Rails 3)将变量发送到部分
【发布时间】:2015-01-07 13:58:12
【问题描述】:

我使用的是 Rails 3,带有标准的 rails 形式和 Cocoon gem。我想使用 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”周围使用方括号/大括号也不起作用。

【问题讨论】:

  • 我没有看到明显的错误(除了关联名称,我假设您只是忘记了前面的 :),这应该可以工作。使用的是哪个版本的 gem?
  • 感谢您的回答。茧 1.2.6

标签: ruby-on-rails ruby-on-rails-3 partials cocoon-gem locals


【解决方案1】:

我有一个非常相似的问题,直到我意识到 render 也在我对 link_to_add_association 的调用上方的 fields_for 块中被调用

这是 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多