Button_to uses POST Link_to uses GET, why? ROR

こ雲淡風輕ζ 提交于 2019-12-12 08:29:48

问题


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"}

回答1:


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 %>



回答2:


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 %>



回答3:


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



来源:https://stackoverflow.com/questions/2167123/button-to-uses-post-link-to-uses-get-why-ror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!