button_to使用POST,link_to使用GET,为什么?ROR

9

我遇到了一个使用link_to的ror问题。为什么我在link_to参数中指定了"method"=>"post"后,我的链接使用GET方法而我的button_to使用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"}
3个回答

13

我认为你的HTML选项必须与URL选项分别放在两个不同的哈希表中:

<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote'}, {:method => :post } %>
我曾经四处寻找一个正式的例子,但都没有找到。对于我的代码,我大多数情况下已经放弃了,只使用了新样式:
<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %>

我在使用ROR 3.0.17(或18、19,记不清了)时遇到了同样的问题。我按照“新样式”制作了标记,它在大多数页面上都能正常工作,但有一个特定的页面除外。调用是完全相同的,我不知道出了什么问题。此外,Firebug显示该链接具有2个数据参数:data-confirm和data-method。这样做是否正确? - Rodrigo Castro

8

对于需要访问的用户可能会有所帮助 :)

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

要执行 GET 操作,语法如下:

<%= button_to 'pdf', { :action => 'getquote'}, :method => :get %>

1
可以工作,但只有在将 :get 作为符号或小写的 "get" 传递时才有效。字符串 "GET" 不起作用。这只是一个小问题。 - Gunchars
3
当我这样做时,会在我的URL末尾添加一个问号“?”。有人看到过这种情况或者有解决方法吗? - Trevor McKendrick

1

有可能是您已禁用了Javascript,此时它将退回到GET。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接