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

  • Home
  • SEARCH
  • 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 8295125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:28:23+00:00 2026-06-08T14:28:23+00:00

I am working on a rails application and it requires two different types of

  • 0

I am working on a rails application and it requires two different types of roles. One is Employee and other is Admin.

Cancan documentation says that it assumes there is a user or current_user method in the application.

So how can I use cancan to set roles for employee and manager in my app ?

  • 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-08T14:28:24+00:00Added an answer on June 8, 2026 at 2:28 pm

    Do like this

    write this in application helper

    def is_employee?(user)
      emp_role = Role.find(:first, :conditions => ["name = ?", "Employee"])
      return user.roles.include?(emp_role)
    end
    
    def is_admin?(user)
      admin_role = Role.find(:first, :conditions => ["name = ?", "Admin"])
      return user.roles.include?(admin_role)
    end
    

    And abily look like this

         class Ability
           include CanCan::Ability
           include ApplicationHelper
    
        def initialize(user)
          # Define abilities for the passed in user here. For example:
           #
          user ||= Employee.new # guest user (not logged in)
          if is_admin?(user)
           can :manage, :all
           # cannot :manage, IpAddress
         elsif is_employee?(user)
    
          #your code
         end
    

    For define roles see it

       http://stackoverflow.com/questions/10222400/rails-adding-an-admin-role-using-devise-who-can-see-all-the-users/10222813#10222813
    

    It sure works…

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

Sidebar

Related Questions

I'm working on a Rails application where I have some a set of two
Background Currently, I am working on a Rails application. I have different products that
The Rails application I'm working on, has two central pieces: users and groups. A
I'm working on a Rails application that requires you to be logged in to
I am working on a rails application that requires files to be uploaded to
I'm working on rails application with postgres db.I have a table called merchant_review_votes where
I am thinking of working on a Rails application that uses PostgreSQL. I have
I'm having working on a Rails application that is supposed to run on an
I am working on JRuby on Rails Application having JRuby-1.6.7 and Rails 3.2.2 I
I'm working in a non-privileged environment, and my Rails application's root url is http://foo.com/bar

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.