9

I've ran into a ror problem using the link_to. Why does my link to use the GET method and my button_to use the POST method, after I specified my "method"=>"post" within the link_to parameters?

View:

<%= button_to "pdf", :action => 'getquote' %>
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote', :method => :post } %>

Controller Method:

def getquote
@cart = find_cart
respond_to do |format|
format.pdf
end
end

Terminal Output (Button/Link, respectively):

Processing InventoriesController#getquote (for 127.0.0.1 at 2010-01-30 01:38:02) [POST]
  Parameters: {"action"=>"getquote", "authenticity_token"=>"D2cwnHyTHgomdUM3wXBBXlOe4NQLmv1Srn0paLbExpQ=", "controller"=>"inventories"}

Processing InventoriesController#show (for 127.0.0.1 at 2010-01-30 01:39:07) [GET]
  Parameters: {"method"=>"post", "action"=>"show", "id"=>"getquote", "controller"=>"inventories"}
JZ.
  • 21,147
  • 32
  • 115
  • 192

3 Answers3

13

I think your html options have to be in a separate hash from your url options:

<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote'}, {:method => :post } %>

I looked all over for a proper example, with no luck. For my code, I've mostly given up and just use the new style:

<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %>
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
Ed.
  • 146
  • 2
  • I'm having this same problem with ROR 3.0.17 (or 18, or 19, don't remember). I made the markup just like this "new style" it's working on most pages except one specific page. The call is the exact same thing, I don't know what's wrong. Also firebug shows that the link has 2 data parameter: data-confirm and data-method. Is this supposed to be right? – Rodrigo Castro Sep 05 '12 at 19:52
8

Might be useful for someone who is visiting :)

By default, button_to performs POST action only.

to do make a GET the syntax is as follows:

<%= button_to 'pdf', { :action => 'getquote'}, :method => :get %>
Prasanna
  • 10,956
  • 2
  • 28
  • 40
1

One possibility is that you have Javascript disabled, in which case it will fall back to a GET.

JRL
  • 76,767
  • 18
  • 98
  • 146