9

我在使用 link_to 时遇到了一个 ror 问题。为什么我的链接使用 GET 方法而我的 button_to 使用 POST 方法,在我在 link_to 参数中指定了我的“method”=>“post”之后?

看法:

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

控制器方法:

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

终端输出(分别为按钮/链接):

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

3 回答 3

13

我认为您的 html 选项必须与您的 url 选项位于单独的哈希中:

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

我到处寻找一个合适的例子,但没有运气。对于我的代码,我大部分都放弃了,只使用新样式:

<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %>
于 2010-02-17T12:00:16.753 回答
8

可能对来访的人有用:)

默认情况下,button_to 仅执行 POST 操作。

做一个 GET 语法如下:

<%= button_to 'pdf', { :action => 'getquote'}, :method => :get %>
于 2012-03-25T14:34:05.670 回答
1

一种可能性是您禁用了 Javascript,在这种情况下,它将退回到 GET。

于 2010-01-30T06:59:54.950 回答