I have a form that doesn't have a model associated with it, so I'm using form_tag
rather than form_for
. As expected, when the user submits the form each of the fields is included in the params hash. But the form will change a lot and I would prefer to have a hash within the params hash that will hold all of the form field values so that I don't have to change my controller every time I change my form.
Is there a way to put the form field values into a nested hash like form_for does? I'd like to be able to take all of the form fields and convert them to json by doing something like params[:form_fields].to_json
You can use fields_for
inside a form_tag
for a more formal way of expressing a namespace.
fields_for :form_fields do |ff|
ff.text_field :my_text_field
ff.select :my_select_tag
end
Alternatively just namespace all your form inputs, as such:
text_field_tag "form_fields[my_text_field]"
select_tag "form_fields[my_select_tag]" ...
etc. Then you will get params[:form_fields] = {:my_text_field => "foo", :my_select_tag => "bar"}
, which I think is what you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With