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 7789665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:21:57+00:00 2026-06-01T21:21:57+00:00

My permissions are defined in a database like described on CanCan wiki . I

  • 0

My permissions are defined in a database like described on CanCan wiki. I want to authorize the :index only for the id in my permissions table, but still display the index page if no resource has been authorized.

If I use load_and_authorize_resource, it loads the resources based on the permissions. If I don’t have any, the index action isn’t accessible. I get an access denied on the resource.

If I still want to allow access to the :index action with no resource authorized, I see two options:

Option 1 (hate it)
: Load manually the resources for the index.

class FirmsController < ApplicationController
  load_and_authorize_resource :except => :index

  def index
    @firms = user.permissions.where{action: ["index", "manage", "read"], subject_class: "Firm"}
  end
end

Option 2 (meh) : Add an ability for the :index with a constraint of id: [] (nothing) and overwrite it with my database permissions afterward.

class Ability
  include CanCan::Ability

  def initialize(user)

    user ||= User.new # guest user (not logged in)

    can :create, Firm
    can :index, Firm, id: []

    user.permissions.each do |permission|
      if permission.subject_id.nil?
        can permission.action.to_sym, permission.subject_class.constantize
      else
        can permission.action.to_sym, permission.subject_class.constantize, :id => permission.subject_id
      end
    end

  end
end

Option 3?

Can you think of a cleaner solution?

  • 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-06-01T21:21:59+00:00Added an answer on June 1, 2026 at 9:21 pm

    As stated in docs you should use helper method:

      skip_authorize_resource :only => :index
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to manage permissions and access to a SQL Server database server on
I want to use CLR table-valued function in SQL Server 2008, which accesses a
I have a many-to-many mapping defined (only relevant fields included) with FluentNHibernate (v1.0.0.637): //
I understand an activity needs to be defined/described in the AndroidManifest xml file. I
I have a question about User-Defined Table Types in SQL Server 2008. For the
I want to use Spring Security to manage user, group and permissions. I want
I have the following table schema which maps user_customers to permissions on a live
I want to extend the permissions given for an user to an entity. I
I changed the file permissions on all my files and directories in my Joomla
How to list all permissions enabled by given module(s)?

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.