views:

152

answers:

3

Hello, 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.

+1  A: 

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).

Greg Dan
+1  A: 

Another option is to override the "name" parameter of the second button.

<%= f.submit "Deny", :name => "commit_deny" %>
erik
Sadly this does not work. When Prototype serializes the form it takes the first submit button it sees.
vrish88
actually it takes both params and then rails only keeps the first. https://prototype.lighthouseapp.com/projects/8886/tickets/672-formserialize-and-multiple-submit-buttons
vrish88
A: 

I think Submit is unique per form (HTML thing) so yopu have two options:

  1. Use 2 forms whith 1 submit each, that lead to the same action (CSS it to you liking)
  2. Use 2 button-type controls (i.e. not submits) and submit with both onClick events (javascript needed)
Fer