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.

0


a source to share


3 answers


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

+1


a source


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



<%= f.submit "Deny", :name => "commit_deny" %>

      

+1


a source


I think Submit is unique for each form (HTML thing), so yopu has two options:

  • Use 2 forms with 1 submission each that lead to the same action (CSS you like it)
  • Use two controls on buttons (i.e. not dispatch) and dispatch both onClick events (javascript required)
0


a source







All Articles