sim*_*mha 6 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2
我在routes.rb文件中有以下代码.
resources :users do
member do
get :following,:followers
end
collection do
put :activate_email
end
end
Run Code Online (Sandbox Code Playgroud)
我有一个用户电子邮件激活链接,如下所示:
<%= link_to "Activate",activate_email_users_url(email_token: @user.email_token),method: :put %>
Run Code Online (Sandbox Code Playgroud)
当我单击激活链接时,这是生成的URL
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
Run Code Online (Sandbox Code Playgroud)
更新:好的,我想我知道问题是什么.当我在我的Gmail中查看包含link_to的激活电子邮件的html源时,没有
data-method='put'.所以这似乎是问题所在.它总是发送默认的GET请求而不是PUT.这是我的user_mailer/registration_confirmation.html.erb文件
<%= javascript_include_tag "application" %>
</head>
Run Code Online (Sandbox Code Playgroud)
请点击以下链接激活您的电子邮件<%= link_to"激活",activate_email_users_url(email_token:@ user.email_token),方法:: put%>
这会出现以下错误:
undefined method `protect_against_forgery?' for #导致此错误.有没有办法解决 ?So , the code <%= javascript_include_tag "application" %>
抱歉,我不知道您的目的,但显然您有激活用户的目的。试试这个,如果这个解决方案不起作用,请告诉我你在控制器上的操作(activate_email)!
见rake routes
输出:
activate_email_users PUT /users/activate_email(.:format) users#activate_email
user GET /users/:id(.:format) users#show
当你生成
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
您的问题被activate_email
认为是:id
users/activate_email => users/:id
以及解决您的问题:
尝试method
从链接中删除。最好method
在您的路由文件中指定。如何通过放入路由替换匹配:
resources :users do
member do
get :following,:followers
end
end
put "/users/activate_email/:email_token" => "users#activate_email", :as => "activate"
Run Code Online (Sandbox Code Playgroud)
并在视图中
<%= link_to "Activate", activate_path(:email_token => @user.email_token) %>
Run Code Online (Sandbox Code Playgroud)
我没有测试过这个,但我想这就足够了。
更新
对于问题:未定义的方法`protect_against_forgery?'
将此添加到仅您的邮件模板使用的帮助程序:
def protect_against_forgery?
false
end
Run Code Online (Sandbox Code Playgroud)
注意:如果您有新问题,请创建新的“提问”并批准答案对这个问题很有用