Button_to使用POST Link_to使用GET,为什么?ROR

JZ.*_*JZ. 9 ruby-on-rails link-to

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

视图:

<%= button_to "pdf", :action => 'getquote' %>
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote', :method => :post } %>
Run Code Online (Sandbox Code Playgroud)

控制器方法:

def getquote
@cart = find_cart
respond_to do |format|
format.pdf
end
end
Run Code Online (Sandbox Code Playgroud)

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

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"}
Run Code Online (Sandbox Code Playgroud)

小智 13

我认为你的html选项必须与你的url选项分开一个哈希:

<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote'}, {:method => :post } %>
Run Code Online (Sandbox Code Playgroud)

我好好看了一遍,没有运气.对于我的代码,我大多放弃了,只使用新的风格:

<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %>
Run Code Online (Sandbox Code Playgroud)


pra*_*ann 7

可能对正在访问的人有用:)

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

做一个GET的语法如下:

<%= button_to 'pdf', { :action => 'getquote'}, :method => :get %>
Run Code Online (Sandbox Code Playgroud)

  • 当我这样做时,它会添加一个问号"?" 到我的URL的末尾.其他人看到这个或有修复? (3认同)