2 buttons, 1 form
I have a simple form that looks like this
<% remote_form_for post, :url => post_path(post), :method => :put do |f| -%>
<%= f.submit "Approve" %>
<%= f.submit "Deny" %>
<% end -%>
What is he doing
<input type="submit" value="Approve" name="commit"/>
<input type="submit" value="Deny" name="commit"/>
In my controller, I have the following logic
@post.approved = params[:commit] == 'Approve' ? true : false
So the problem is that if the user clicks the Approve button or the Decline button, the parameter sent is that :commit => "Approve"
.
Does anyone know of a bug related to some (simple) way of doing the same functionality?
Thanks.
a source to share
JS lib (Prototype I guess) doesn't know which button was pressed. It just serializes the values ββof the form fields for the Ajax request. When using the normal POST form, browsers assign the correct value to the commit parameter.
You can add a hidden form field (like an action). Then add some JS code to set the required hidden field value when the corresponding button is clicked (and before the Ajax request is sent).
a source to share