2013-10-26 45 views
0

查看:Rails工作與輸入標籤打器控制器提交

%input{type: 'submit', action: 'home#create_user'} 

控制器:

class HomeController < ApplicationController 
    def index 
    render 'home/index' 
    end 

    def sign_up 
    render 'home/sign_up' 
    end 

    def create_user 
    render 'dashboard/dashboard' 
    end 
end 

的routes.rb

post 'home/create_user' => 'home#create_user', :as => :create_user 

爲什麼這個按鈕不打控制?

回答

2

本身,提交標記不會生成表單。我上面試過你的代碼,按鈕沒有做任何事情。除非我忘記INPUT沒有ACTION屬性。

如果您有指向該頁面的鏈接,則說明該頁面無法正常工作是因爲默認情況下,該鏈接將成爲GET請求,並且您已將路由限制爲POST。

因此,無論是將其封裝在表單中,還是使用button_to或:method =>:post解決方案來使其POST請求,它應該可以工作。

事情是這樣的:

= button_to 'click me', create_user_path 
= link_to 'click me', create_user_path, method: 'post' 
+0

你的意思是這樣嗎? %input {type:'submit',action:'home#create_user',method:'post'} – joncodo

+0

這應該很容易。我只是試圖點擊一個按鈕,並將參數發送到控制器,並從文本框中設置參數。我在這裏錯過了一些基本的東西嗎?我可以用表單標籤來做到這一點,但我不想。 – joncodo

+0

@JonathanO號我已將代碼示例添加到我的答案中。 –