Devise: ActionView::Template::Error (undefined method `protect_against_forgery?'

Created on 23 Mar 2016  ·  5Comments  ·  Source: heartcombo/devise

Using Rails 5 beta3 API mode.

Getting this error when trying to do a get request to /users/sign_in route

Most helpful comment

I had same problem, but succeeded to fix it.

  • protect_against_forgery? is defined in ActionController::RequestForgeryProtection module. This module is included by ActionController::Base.
  • By default, DeviseController extends 'ApplicationController'.

ApplicationController usually extends ActionController::Base.
On Rails5 API mode, however, ApplicationController extends ActionController::API, which does not include ActionController::RequestForgeryProtection.

In order to fix, I inserted config.parent_controller = 'ActionController::Base' into config/initializers/devise.rb.

All 5 comments

Hey! I'm having the same problem

How did you fix this?

+1. How did you fix this?

I had same problem, but succeeded to fix it.

  • protect_against_forgery? is defined in ActionController::RequestForgeryProtection module. This module is included by ActionController::Base.
  • By default, DeviseController extends 'ApplicationController'.

ApplicationController usually extends ActionController::Base.
On Rails5 API mode, however, ApplicationController extends ActionController::API, which does not include ActionController::RequestForgeryProtection.

In order to fix, I inserted config.parent_controller = 'ActionController::Base' into config/initializers/devise.rb.

How you configure to all others actions to respond to JSON instead HTML when you changed to config.parent_controller = 'ActionController::Base'?

@rodriggochaves
I don't longer use devise with Rails5 API mode, so not remember well what I did...
I recognize that config/initializers/devise.rb effects only modoules which relate to devise,
so I think config.parent_controller = 'ActionController::Base' not change other controller's work.

if you will use Rails5 API mode, I recommend to devise_token_auth. I already use it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Pedroknoll picture Pedroknoll  ·  3Comments

HerbertKoelman picture HerbertKoelman  ·  3Comments

lancecarlson picture lancecarlson  ·  3Comments

edipox picture edipox  ·  4Comments

neohunter picture neohunter  ·  3Comments