Reputation: 195
I need to be able to create a form where there are several text_field_tags that are created dynamically depending on the instances of another model. I want to be able to store the values that are entered into these text_field_tags into the same array that I can access as a parameter in the controller once the form is submitted. Is this possible?
Upvotes: 3
Views: 6425
Reputation: 1326
If you give them all the same name and append [] to the end as follows:
<%= text_field_tag "some_fields[]" %>
<%= text_field_tag "some_fields[]" %>
<%= text_field_tag "some_fields[]" %>
You can access these from the controller:
some_fields = params[:some_fields] # this is an array
If you enter values between the square brackets, rails will view it as a hash:
<%= text_field_tag "some_fields[1]" %>
<%= text_field_tag "some_fields[2]" %>
<%= text_field_tag "some_fields[3]" %>
would be interpreted by the controller as a hash with keys "1", "2" and "3"
Upvotes: 18