2014-12-19 104 views
0

我試圖把一些光滑的按鈕放在我的rails應用程序中作爲鏈接。但是,我嘗試這樣做時遇到了一個奇怪的問題。我已經嘗試添加一個鏈接按鈕如下:Button_to鏈接導致路由錯誤

<%= button_to("New", :action => "new", :controller => "registrations") %> 

這將導致一個不錯的新按鈕,我的用戶引導至sign_up頁面。

這裏的地方會很奇怪:當我點擊我排到http://localhost:3000/users/sign_up按鈕,收到以下錯誤:

No route matches [POST] "https://stackoverflow.com/users/sign_up"

但是這根本不是真的。事實上,我可以突出顯示導致我出現此錯誤的網址,將其複製並粘貼到新標籤中,並加載正常。

必須絕對清楚,這裏是從rake routes路徑:

new_user_registration GET /users/sign_up(.:format)  registrations#new 

什麼可能是怎麼回事呢?

任何想法表示讚賞。

回答

3

您的路由期望方法得到的地方,因爲button_to`不應該發送GET請求, 這就是創建問題。

你必須做的事情之後一個

1.更改button_tolink_to

<%= link_to("New", :action => "new", :controller => "registrations") %> 

2.增加:method => :get

<%= button_to("New", {:action => "new", :controller => "registrations"}, :method => :get) %> 
+0

很好,謝謝! – neanderslob 2014-12-19 08:32:18

1

默認情況下,點擊按鈕發送POST請求服務器。你應該改變這種行爲發送GET

<%= button_to('New', {action: 'new', controller: 'registrations'}, method: :get) %>