Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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'
. 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>

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

This gives the following error :

undefined method `protect_against_forgery?' for #

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

is causing this error. Is there any way around this ?
like image 766
simha Avatar asked May 21 '13 14:05

simha


1 Answers

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

like image 102
rails_id Avatar answered Sep 21 '22 12:09

rails_id