Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form_tag parameters in nested hash

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

like image 388
Kevin Thompson Avatar asked Feb 27 '13 15:02

Kevin Thompson


1 Answers

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.

like image 150
Mike Campbell Avatar answered Nov 01 '22 18:11

Mike Campbell