Reputation: 6259
For one form, I want to have two submit buttons , named:
Appove
, Decline
when the user clicks on Approve
it should send approve: true
as well
when the user clicks on Decline
it should send approve: false
as well
Basically the buttons should work like a checkbox
Is this possible with Rails and how to implement it? Thanks
Upvotes: 0
Views: 661
Reputation: 36860
If you're using the standard submit
form helper, you will get returned a param with the key "commit". You can test for this in your controller code.
<%= f.submit 'Approve' %>
<%= f.submit 'Decline' %>
in the controller...
def create
approved = params[:commit] == 'Approve'
The approved
variable will then contain true or false and you can use it as needed in the rest of the action / method.
Upvotes: 2
Reputation: 414
You can do it but you need js/jquery for this. You will have hidden checkbox that you will check in proper way and two buttons. Lets assume that your form has id 'form'. And you checkbox has id 'approve_checkbox' In this case you need submit function for something like this.
$('#approve_button').on('click', function(e){
e.preventDefault();
$('#approve_checkbox').prop('checked', true);
$('#form').submit();
});
$('#decline_button').on('click', function(e){
e.preventDefault();
$('#approve_checkbox').prop('checked', false);
$('#form').submit();
});
Of course you can simplify this code, but I think idea is clear.
Upvotes: 1