0

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 1

3

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.

Sign up to request clarification or add additional context in comments.

17 Comments

Thanks. I'm relatively new to this so this is a little confusing for me. I don't see where I'll need the attribute and what that exactly is. As of now I have an array called "order" and I have this line = f.submit "Save Changes", :method => :sort, class: "button btn btn-primary w-100" How can I send the list order to the order array so that the sort method in the controller can use it. And how would I access it from within the controller?
Just paste your form and what you want to send as array so I can help further. The question doesn't has any details.
I put part of the code in the question. I want to send order to sort method.
I'm getting undefined local variable or method `order' for #< <Class:0xa9f0638>:0x3b3c208> from the code I add to the form.
Underneath the 2nd line
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.