Search code examples
ruby-on-railsrubybooleansubmitcheckmark

How to Create Accept/Decline Buttons with Boolean?


I have accept/decline as a switch:

<%= form_for @dueler do |f| %>
  <%= f.check_box :accept, class: 'someclass', :data => { 'on-text'=>'Decline', 'off-text'=>'Accept'} %> 
  <script>
    $("[class='someclass']").bootstrapSwitch();
  </script>
  <%= button_tag(type: 'submit')  do %>
    Save
  <% end %>
<% end %>

But I'm trying to make them into two separate buttons. If a user clicks one, how to make it where it would automatically submit with the appropriate :accept value:

<%= button_tag(type: 'submit')  do %>
  Accept # submits & makes :accept true
<% end %><br><br>

<%= button_tag(type: 'submit')  do %>
  Decline # submits & makes :accept false
<% end %>

Solution

  • You can use f.button to pass a param that you'll use in the controller:

    In your view:

    <%= f.button "Accept", name: "button_action", value: "accept" %>
    <%= f.button "Decline", name: "button_action", value: "decline" %>
    

    In your controller use the param value:

    if params['button_action'] == 'accept'
    else
    end