I have a simple form that looks like so
<% remote_form_for post, :url => post_path(post), :method => :put do |f| -%>
<%= f.submit "Approve" %>
<%= f.submit "Deny" %>
<% end -%>
Which renders
<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 problem is that if the user clicks the “Approve” button or the “Deny” button the parameter that is sent is that :commit => "Approve".
Does anybody know of a bug relating to this or another (simple) way to perform the same functionality?
Thanks.
JS lib (Prototype I guess) doesn’t know what button was pressed. It just serializes the form field values for the Ajax request. When using normal form POST, browsers attach right value to the commit param.
You can add hidden form field (eg action). Then add JS code to set required value of the hidden field when appropriate button is pressed (and before the Ajax request is sent).