undefined method `protect_against_forgery?' for #<#<Class:0x0

无人久伴 提交于 2019-12-01 17:53:22

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!

see on rake routes output :

activate_email_users PUT /users/activate_email(.:format) users#activate_email user GET /users/:id(.:format) users#show

when your generate

http://localhost:3000/users/activate_email?email_token=WWNvMN-r_lXgovrQiDlSSQ

Your problem was activate_email considered to be :id

users/activate_email => users/:id

And solution for your problem :

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"

and on view

<%= link_to "Activate", activate_path(:email_token => @user.email_token)  %>

I have not tested this, but I guess this will suffice.

UPDATE

for Question : undefined method `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

If you're trying to activate a single user account you probably don't want to be specifying your route on the collection (which you would use for actions that operate on multiple users).

Here's some (untested) code that should point you in the right direction:

controller :users do
    put '/activate/:email_token', :to => :activate,  :as => 'activate_email'
end

Which should route a PUT to /activate/xxxx to the UsersController#activate action with a params[:email_token] set as xxxx. It should also give you a #activate_email_url route which you can pass the activation token (you can check what routes your app provides by running rake routes on the command line).

Google redirected me to this question even-though mine was related to rendering a template into a string and not just in the browser. My solution for the template problem was something along these lines:

  action_controller = ActionController::Base.new()

  action_controller.class_eval do
    def protect_against_forgery?
      false
    end
  end

  file_string = action_controller.render_to_string('/some_template/template_file',locals: { local_variable: 1 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!