2010-01-30 59 views
8

我已經遇到了使用link_to的ror問題。在link_to參數中指定我的「method」=>「post」之後,爲什麼我的鏈接使用GET方法和我的button_to使用POST方法?Button_to使用POST Link_to使用GET,爲什麼? ROR

查看:

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

回答

11

我覺得你的HTML選項必須在一個單獨的哈希從您的網址選項:

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

我到處尋找一個適當的例子,沒有運氣。對於我的代碼,我大部分放棄了,只是使用了新樣式:

<%= link_to 'Delete', custom_event, :confirm => 'Are you sure?', :method => :delete %> 
+0

我與ROR 3.0.17有同樣的問題(或18或19,不記得)。我做了這種標記,就像這個「新風格」,它在大多數頁面上工作,除了一個特定的頁面。電話是完全一樣的,我不知道什麼是錯的。另外螢火蟲顯示鏈接有2個數據參數:數據確認和數據方法。這應該是正確的? – 2012-09-05 19:52:54

1

一種可能性是,你禁用了JavaScript,在哪種情況下會回退到GET。

+1

啓用JavaScript。 – 2010-01-30 20:29:30

6

可能是誰正在訪問:)

默認情況下是有用的人,button_to只執行POST操作。

做使得到語法如下:

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

可以工作,但只有當您將':get'作爲符號或小寫'「get」'時纔會生效。字符串''GET「'不起作用。只是一個小問題。 – Gunchars 2013-11-15 21:04:15

+3

當我這樣做時,它添加一個問號「?」到我的URL的末尾。任何人看到這個或有修復? – 2014-02-24 05:18:05

+1

@TrevorMcKendrick可能是這個有用http://stackoverflow.com/a/8122116/1297435 – 2016-08-22 02:51:30

相關問題