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

I have the following code in the 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:

 <%= link_to "Activate",activate_email_users_url(email_token: @user.email_token),method: :put %> 

When I click on the activation link, this is the URL that is generated

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

Update: Good, so I think I know what the problem is. When I look at the activation email html source in my gmail that contains link_to, no

  data-method = 'put' 
. So this is a problem. It always sends a default GET request instead of PUT. This is my user_mailer / registration_confirmation.html.erb file
  <%= javascript_include_tag "application" %> </head> 

Please click on the following link to activate your email address. <% = link_to "Activate", activate_email_users_url (email_token: @ user.email_token), method :: put%>

This results in the following error:

  undefined method `protect_against_forgery? '  for #

So, the code <% = javascript_include_tag "application"%>

causes this error. Is there any way around this?
+6
source share
3 answers

Sorry, I don’t know your goal, but apparently you have a goal to activate the user. Try it if this solution does not work, tell me your action (activate_email) on the controller!

see rake routes output:

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

when you create

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

Your problem was activate_email considered :id

users/activate_email => users/:id

And the solution for your problem:

Try removing the method from the link. Its best to specify method in your routes file. How about replacing matching paths in routes like:

 resources :users do member do get :following,:followers end end put "/users/activate_email/:email_token" => "users#activate_email", :as => "activate" 

and in view

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

I have not tested this, but I think that will be enough.

UPDATE

for the question: undefined method `protect_against_forgery? ''

Add this to an assistant that uses only your mailer template:

  def protect_against_forgery? false end 

NOTE If you have a new question, please create a new β€œAsk Question” and aprrove answer is useful for this question.

+4
source

If you are trying to activate a single user account, you probably do not want to indicate your route in the collection (which you will use for activities that work with multiple users).

Here is some (unverified) code that should point you in the right direction:

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

Why do we need to direct the PUT to /activate/xxxx in the UsersController#activate action using params[:email_token] set as xxxx . It should also provide you with the #activate_email_url route through which you can pass the activation token (you can check which routes your application provides by running rake routes on the command line).

0
source

Google redirected me to this question, although mine was related to displaying the template in a string, and not just in the browser. My solution to the pattern problem was something like this:

  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 } 
0
source

Source: https://habr.com/ru/post/1481968/


All Articles