【问题标题】:Ruby On Rails - Simple_fields_for with nested_form, pass variableRuby On Rails - Simple_fields_for 与nested_form,传递变量
【发布时间】:2013-10-02 12:39:54
【问题描述】:

嵌套表单与简单表单一起使用时是否可以传递变量?

喜欢

<%= simple_nested_form_for(@foo) do |f| %>
  ...
  <%= f.simple_fields_for :bar %>
  <%= f.link_to_add :bar do %>
    Add bar
  <% end %>
<% end %>

我试过了

<%= f.simple_fields_for :bar, :locals => {:baz => 'baz'} %>

但它不会在部分中拾取它。

部分:_bar.html.erb

<%= baz %>

Simple Form Gem

Nested Form Gem

【问题讨论】:

  • 我在您的代码中看不到任何对部分的引用。您要使用哪个部分?
  • @FarleyKnight - 已更新。

标签: ruby-on-rails variables nested-forms simple-form partial


【解决方案1】:

我一直在寻找同样的东西,但从未在任何地方看到答案,所以我发布了一个对我有用的示例:

<%= f.simple_fields_for :answers do |answers| %>
   <%= render 'answer_fields',  { f: answers, question: @question }  %>
<% end %>

另外,如果你想从在局部渲染的集合中访问当前对象,请在局部中使用 f.object。

【讨论】:

    【解决方案2】:

    要将变量传递给局部变量,请使用:locals 选项:

    <%= render partial: "my_awesome_partial", locals: {variable: 5, baz: 'baz'} %>
    

    【讨论】:

    • 只是一个错字,问题是我不能用 simple_fields_for 传递这个,普通的部分工作得很好......
    • 表单助手不使用部分。您自己指定部分,您希望它们在您的视图中。我需要查看您正在使用的教程或更多代码,以了解您正在尝试做什么。
    • 我正在尝试访问嵌套表单的父母属性。如果您看一下:github.com/ryanb/complex-form-examples/tree/nested_form/app/… 您会看到 Ryan 正在使用他的 gem nested_form 添加新问题,我希望能够将变量发送到带有问题字段的部分,并且还能够将变量发送到答案字段。
    猜你喜欢
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多