Philip
Philip

Reputation: 7166

Ruby On Rails - Simple_fields_for with nested_form, pass variable

Is it possible to pass variable when using nested form with Simple form?

Like

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

I have tried

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

but it wont pick it up in the partial.

Partial: _bar.html.erb

<%= baz %>

Simple Form Gem

Nested Form Gem

Upvotes: 0

Views: 1097

Answers (2)

SharonM
SharonM

Reputation: 41

I was looking for the same thing and never did see an answer anywhere, so I'm posting an example of what worked for me:

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

Also, if you want to access the current object from the collection being rendered within the partial, use f.object within the partial.

Upvotes: 2

Farley Knight
Farley Knight

Reputation: 1793

To pass variables to partials, you use the :locals option:

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

Upvotes: 1

Related Questions