Dynamic content with fields_for

I have a nested form that uses has_many relationships. In my form view, I use partial for field inputs and pass in a FormBuilder object.

form.html.haml:

- form_for @record do |f| .field = container do - f.fields_for :strings do |s| = render :partial => 'string_fields', :locals => {:s => s} 

_string_fields.html.haml:

 = s.hidden_field :id = s.hidden_field :language_id .field %h3 = t(:name) = s.text_field :name, :size => 50 .field %h3 = t(:description) = s.text_area :description, :rows => 6 

It works as it should; however, I would like to add functionality in AJAX to dynamically add another set of fields using RJS, and when I try to display partial through RJS, obviously s is undefined (I don't know, to pass through the hash :locals ).

Is there a way to correctly add a field dynamically to the set of fields defined by fields_for , or do I need to override my partial without using helpers?

+4
source share
1 answer

You can dynamically add fields with your current implementation, see:

http://railscasts.com/episodes/197-nested-model-form-part-2

for inspiration

+6
source

Source: https://habr.com/ru/post/1346280/


All Articles