I have an edit form in which I build an array of numbers. When I click submit, I will do " :method => :sort ". How do I make it so my sort method in my controller can use the array. The edit form is done in haml.
1 Answer
There are more solutions to it but I prefer this one:
In the model for which the form is for add:
attr_accessor :order_ids
Now in the haml code i.e., in the form add:
%input{name: "model_name[order_ids][]", type: "hidden", value: order}
And in the controller in:
params[:model_name][:order_ids]
you should get the array. Just change the model_name to the name of you model.
And why are you sending:
= f.submit "Save Changes", :method => :sort
method as sort. This is not correct. If you want to send the form to the action sort in your controller then that needs to be specified in the form url
Suppose you have a action named sort in lists_controller and in your routes you define it as:
put '/sort', to: 'lists#sort', as: list_sort
Then in the form you can give it as:
= form_for @list, :html => { :method => :put, :multipart => true, url: list_sort_path } do |f|
Hope this helps.