I’m starting to develop a small application in Ruby On Rails. I have a short form (within a view) in which the number of fields is variable and created with the code snippet below:
<% for item in @event.items %>
<tr>
<td><%= item.name %></td>
<td><input type="number" name="quantity" /></td>
</tr>
<% end %>
I want to after click ‘submit’, run a controller’s method that access to the data included in each of the fields in the form and perform insertions on the database. In the end, I want the page to be refreshed.
How do I customize the action of the submit button to perform a controller’s function? And how do I access inside this function to the form fields (whose number is variable as mentioned).
Thanks in advance for your help.
You can use nested attributes. Then it should looks something like this (sorry for HAML):
And in your event model:
I would suggest to use in this case the nested_form gem https://github.com/ryanb/nested_form.