ruby-on-railsparameter-passingputlink-tobutton-to

Sending parameters through button on rails form_tag with put method


I have two links ( could be buttons if needed ) that say accept and decline and I need to send true or false parameters to my controller action by clicking one of those links. I don't want my parameters to be visible in the url so I need to use put method. I have tried with the link_to with defined method:

<%= link_to 'accept', { action: 'accept_offer', accept: true }, method: :put %>
<%= link_to 'decline', { action: 'accept_offer', accept: false }, method: :put %>

but my params are still visible.

I've tried using button_to but then my parameters are not passed. What is the best way to determine which option has been chosen (accept or decline) without showing parameters in url?

my route has be defined like this:

put 'offers', to: 'offers#accept_offer'

Solution

  • i'll recommend to make a form instead of link_to and pass params in name. Use a form and POST the information.This might require additional code in source pages, but should not require logic changes in the target pages.

    <% form_for @offer, :url => {:action => 'accept_offer'} do |f|%>
      <%= submit_tag "", :value => "Accept", :name => "accept" %>
      <%= submit_tag "", :value => "Decline", :name => "decline"  %>
    <% end %>
    

    in your action you'll get params[:accept] or params[:decline] based on the link you clicked.

    Edited to include commas and spaces with keyword arguments on submit tag.]