I'm trying to figure out how to use check boxes to select multiple
records and update them accordingly depending on what button a user
presses (think GMail http://i.imgur.com/94DUi.jpg e.g., delete, archive,
etc).
There's a really great Railscasts
(#52 Update through Checkboxes - RailsCasts) that
introduced me to this functionality but I'm having trouble figuring out
how to extend it to include more than one button. What is the best way
to go about extending this functionality to include more buttons without
adding the checkboxes? I'm also willing to try an AJAX solution if
someone can link me a good tutorial.
Forms send the name/value pairs of all the input elements to the server.
Forms also send the name/value of the button that was clicked. If you
examine the railscast screenshot where he is checking the log to see
what the checkbox parameters are being sent in the request, you will
also see this parameter:
'commit' => 'Mark as Complete'
'Mark as Complete' is the text on the button, so you can deduce that
must be a name/value pair for the button. If 'Mark as Complete' is the
value, what is the name part of that name/value pair? Answer: 'commit'.
Where does the name 'commit' come from? IN the railscast he never names
the button--all he does is write this:
<%= submit_tag "Mark as Complete" %>
But if you check the docs for submit_tag(), rails give the button the
name 'commit'.
If you name all your buttons 'commit', then in your action to determine
what button was clicked, all you have to do is write:
case params[:commit]
when "text on button1"
#do something
when "text on button2"
#do something else
when "Mark as Complete"
#do that
else
#Error
end