问题描述
我的 routes.rb 文件中有以下代码.
I have the following code in my routes.rb file .
resources :users do
member do
get :following,:followers
end
collection do
put :activate_email
end
end
我有一个像这样的用户电子邮件激活链接:
And I have a user email activation link like this :
<%= link_to "Activate",activate_email_users_url(email_token: @user.email_token),method: :put %>
当我点击激活链接时,这是生成的网址
When I click on the activate link , this is the url that is generated
http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ
更新:好的,所以我想我知道问题是什么.当我查看我的 gmail 中包含 link_to 的激活电子邮件的 html 源代码时,没有
Update: Ok, So I think I kno what the problem is . When I look at the html source of the activation email in my gmail which contains the link_to , there is no
data-method='put'
.所以这似乎是问题所在.它总是发送默认的 GET 请求而不是 PUT.这是我的 user_mailer/registration_confirmation.html.erb 文件
. So that seems to be the problem . It is always sending a default GET request instead of PUT.This is my user_mailer/registration_confirmation.html.erb file
<%= javascript_include_tag "application" %>
</head>
请点击以下链接激活您的邮箱<%= link_to "Activate",activate_email_users_url(email_token: @user.email_token), 方法: :put %>
Please click on the following link to activate your email <%= link_to "Activate",activate_email_users_url(email_token: @user.email_token), method: :put %>
这给出了以下错误:
undefined method `protect_against_forgery?' for #
So , the code <%= javascript_include_tag "application" %>
导致了这个错误.有没有办法解决这个问题?
is causing this error. Is there any way around this ?
推荐答案
抱歉,我不知道您的目的,但显然您有激活用户的目的.试试这个,如果这个解决方案不起作用,请告诉我你在控制器上的操作(activate_email)!
Sorry, I do not know your purpose, but apparently you have a purpose to activate user.Try this, if this solution not work, please tell me your action (activate_email) on controller!
查看 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
Your problem was activate_email
considered to be :id
users/activate_email =>users/:id
以及解决您的问题:
尝试从链接中删除 method
.最好在路由文件中指定 method
.如何通过放入路由替换匹配:
Try removing the method
from the link. Its better specifying the method
in your routes file. How about replacing match by put in routes as :
resources :users do
member do
get :following,:followers
end
end
put "/users/activate_email/:email_token" => "users#activate_email", :as => "activate"
并在视图中
<%= link_to "Activate", activate_path(:email_token => @user.email_token) %>
我还没有测试过这个,但我想这已经足够了.
I have not tested this, but I guess this will suffice.
更新
对于问题:未定义的方法`protect_against_forgery?'
将此添加到仅您的邮件模板使用的帮助程序:
Add this to a helper that only your mailer template uses:
def protect_against_forgery?
false
end
注意:如果您有新问题,请创建新的提问"并批准答案对这个问题很有用
NOTE : If You have new question, please create new "Ask Question" and aprrove answer is usefull for this question
这篇关于未定义的方法“protect_against_forgery?"对于 #<#<Class:0x0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!