Rails - Gem Cancancan and Devise

Asked

Viewed 53 times

0

I use the Gems 'Devise' and 'cancancan' and also the 'rails_admin' in my project.

All of them for the administrative part of the site. This part is working perfectly.However in my main page of the site(root) should not need authentication to access it,but is being requesting the login to enter it.

I added before_action :authenticate_user! in the application_controller as recommended by Devise,.

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

1 answer

1


Every time the function authenticate_user! is called, so Devise requires you to need a user.

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

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

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

Thus, only controllers that inherit from AdminController would require Devise authentication.

  • Thanks,had already solved. However his explanation was very good,better than the one I had received!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.