Rails - Gem CanCanCan and Devise

0

I use gems 'devise' and 'cancancan' and also 'rails_admin' in my project.

All of them for the administrative part of the site. It is working perfectly this part. But in my main page of the site (root) it should not need authentication to access it, but it is being requested the login to enter it. / p>

I added before_action :authenticate_user! to the application_controller as recommended by devise, I guess that might be why.

Anyway, I would like to know how restrictive and require login only on some pages of the site. And not in which users should access without the need of a login, such as ROOT

    
asked by anonymous 27.05.2018 / 18:44

1 answer

1

Whenever the authenticate_user! function is called, then Devise forces you to need a user.

What I would do in this case is to create a ApplicationController only for your part that needs authentication, as follows:

# app/controllers/admin_controller.rb
class AdminController < ApplicationController
  before_action :authenticate_user!
end

Modify all controllers that require authentication to inherit AdminController instead of ApplicationController and remove before_action :authenticate_user! from ApplicationController .

In this way, only controllers that inherit from AdminController would require Devise authentication.

    
07.06.2018 / 02:29