When I try to create custom rails, says ActionController :: InvalidAuthenticityToken


Updated:
Problem resolved
I just needed to put protect_from_forgery in the Users controller. Thanks to everyone.


Rails 4.0.2
When I try to create a new entry in the Users table, I get this message in the browser:

 ActionController::InvalidAuthenticityToken in UsersController#create ActionController::InvalidAuthenticityToken 

But this happens in the Chrome (32.0.1700.107) and Opera (12.16) browsers. In Firefox (27.0.1) and IE 10.0.13 everything works fine. It may not matter, but I have to say that I also use has_secure_password (bcrypt_ruby).

Rails Magazine:

 ... Started POST "/users" for 127.0.0.1 at 2014-02-19 10:26:05 +0400 Processing by UsersController#create as HTML Parameters: {"utf8"=>"βœ“", "authenticity_token"=>"93jpgxCSY3XzZkIJKraOodyObBoaPoPMVz3RiOVBL10=", "user"=>{"name"=>"", "surname"=>"", "patronymic"=>"", "email"=>"", "address"=>"", "phone"=>"", "phone2"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>" "} Can't verify CSRF token authenticity Completed 422 Unprocessable Entity in 2ms ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): actionpack (4.0.2) lib/action_controller/metal/request_forgery_protection.rb:163:in `handle_unverified_request' actionpack (4.0.2) lib/action_controller/metal/request_forgery_protection.rb:170:in `handle_unverified_request' actionpack (4.0.2) lib/action_controller/metal/request_forgery_protection.rb:177:in `verify_authenticity_token' ... 

View the users/new.html.slim :

 = stylesheet_link_tag 'users' .new_user_container = form_for @user do |f| = f.label "" br = f.text_field :name br = f.label "" br = f.text_field :surname br = f.label "" br = f.text_field :patronymic br = f.label "" br = f.text_field :password br = f.label " " br = f.text_field :password_confirmation br br = f.submit " " 
+7
ruby ruby-on-rails ruby-on-rails-4 actionview
source share
2 answers

I just needed to put protect_from_forgery in the Users controller. Thanks to everyone.

 class UsersController < ApplicationController protect_from_forgery def index #@users = User.all.includes(:roles) @users = User.all end def show end def new @user = User.new end def create @user = User.new user_params puts @user.errors.inspect if @user.save flash[:notice] = "  " redirect_to :users else flash[:notice] = "  " render file: :'users/user_error' end flash["notice"] = "Test notice" #redirect_to :users end end 
+6
source share

I met the same problem. This only happens in the Chrome browser. But my problem is that I disable Cookies in Chrome Content Settings . So just turn it on or select Clear on quit if you must disable it.

NOTE. This option appears in the Chrome browser on another device. If you sign in to Chrome with your Google Account, Chrome will sync your settings between devices. Therefore, it can be difficult to understand the problem.

Hope this helps other people who deal with this issue.

+1
source share

All Articles