Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:22:25+00:00 2026-05-30T14:22:25+00:00

I am using devise with CanCan. I am using my user model. My user

  • 0

I am using devise with CanCan. I am using my user model.
My user index page is localhost:3000/users (It is enabled just for :administrator role).

The problem is that CanCan (or Devise) is not checking for authorization on this route. All other routes (i.e. localhost:3000/tasks) are being checked. i.e. If I logout of the system and type the users index page it displays its content. If I type the tasks route it redirects me to the login screen (correct behaviour).

I think that this is happening because of Devise’s routes.
My simplified User model is:

class User < ActiveRecord::Base
   has_and_belongs_to_many :roles

   # Include default devise modules. Others available are:
   # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and           :omniauthable
   devise :database_authenticatable,
      :recoverable, :rememberable, :trackable, :validatable

    # Setup accessible (or protected) attributes for your model
    attr_accessible :name, :role_ids, :role, :email, :password, :password_confirmation, :remember_me

    def role?(role_check)
      self.roles.each do |role|
        return true if (role.name.eql? role_check.to_s.humanize )
      end    

      return false
    end


     def role=(role_id)
        self.roles.clear
        self.roles << Role.find(role_id)
     end

     def role
         self.roles.first unless self.roles.length == 0
     end

   end

My Routes are the following:

devise_for :users
resources :users
devise_for :users,  :controllers => { :registrations => "users/registrations" }

My ability.rb is the following (I haven´t defined yet, it is allowing all):

class Ability
  include CanCan::Ability

  def initialize(user)
   user ||= User.new # guest user

   if user.role? :administrator
       can :manage, :all

   elsif user.role? :department_header
      can :manage, :all
   elsif user.role? :staff
      can :manage, :all
   end
 end
end

How can I fix this?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:22:26+00:00Added an answer on May 30, 2026 at 2:22 pm

    My problem was on the following line:

    load_and_authorize_resource :only => [:show,:new,:destroy,:edit,:update]
    

    I have to include :index

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities. User and Role. I am using Devise and CanCan. They
I have already created a User Model using devise, but I now want to
I'm using Cancan, Devise, Rails 3 for my ordering application. Each user has many
I'm quite new to this and I'm using cancan + devise for my user
Using devise , I have a User model. I do not have a user
I'm using devise to handle my users and as part of my application each
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
Using Rails 3.1.3 with Devise 1.5.3. My app has accounts and users. Each account
I am using devise for my authentication system and on the main page I
I've been following this guide on the Separate Role Model implementation in CanCan .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.