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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:07:15+00:00 2026-06-11T09:07:15+00:00

and start to learn cancan+devise i have ‘users’ table ( devise ) i have

  • 0

and start to learn cancan+devise

i have ‘users’ table ( devise )

i have ‘posts’ table ( with ‘user_id’ field )

i have ‘roles’ table ( with ‘name’ field )

  • 1 – admin
  • 2 – user

i have users_roles ( with ‘user_id’ and ‘role_id’ )

i create 2 users with ‘user’ role

and create 1 user with ‘admin’ role

user.rb
  has_many :posts
  has_many :users_roles
  has_many :roles, :through => :users_roles

role.rb
  has_many :users_roles
  has_many :users, :through => :users_roles

users_role.rb
  belongs_to :user
  belongs_to :role

and there is a question:

i create ability.rb 

with

  def initialize(user)
    user ||= User.new
    if user.persisted?
        #loged in
        can :read, Post
        can :create, Post
        can :update, Post , :user_id => user.id
        can :destroy, Post , :user_id => user.id
    else
        #not logged
        can :read, Post
    end

in my views/posts/index.html.erb

<% @posts.each do |post| %>
  <tr>
    <td><%= post.user.email %></td>
    <td><%= post.title %></td>
    <td><%= post.text %></td>
    <td><%= link_to 'Show', post %></td>
      <% if can? :update, Post %>
        <% if current_user.id == post.user.id %>
          <td><%= link_to 'Edit', edit_post_path(post) %></td>
        <% end %>  
      <% end %>
      <% if can? :destroy, Post %>
        <% if current_user.id == post.user.id %>
          <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <% end %>
    <% end %>  
  </tr>

<% end %>
</table>

<br />
<!-- ???почему Post ? -->
<% if can? :create, Post %>
  <%= link_to 'New Post', new_post_path %>
<% end %>  

and in this case i check, if user login – he can read and create,update,destroy ( if he is autor ), if user not logged(guest) – can only read

but i don’t know how change my ability.rb to do that:

  • i have guest ( only read )
  • i have user ( can read and create,update,destroy ( if he is autor ) )
  • i have one admin ( can read, create, update, destroy )

note i already have role table ( with 2 roles ), and 3 users ( 1 with admin role, 2 with user role )

  • 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-11T09:07:16+00:00Added an answer on June 11, 2026 at 9:07 am

    This is how I implemented cancan in my application to manage roles and a guest user.

    Simply use a if user.role == role to verify that the user has the right role.

    class Ability
    include CanCan::Ability
    
      def initialize(user)
        user ||= User.new # guest user
        if user.role == "author"
          # 
          can :read, Post
          can :create, Post
          can :update, Post , :user_id => user.id
          can :destroy, Post , :user_id => user.id
    
          # a simple way to realize read create update and destroy is :manage
          # can :manage, Post, :user_id => user.id
    
        else
          if user.role == "admin"
            # User with role admin can manage all on all models 
            can :manage, :all
          else
            # Guest user can only read something
            can :read, Post
            # or
            # can :read, [SomeModel1, Somemodel2]
            # can :read, :all     # this means he can read all models 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just start to learn wpf. Can anybody clear me the following to
I start learn EF Fluent API. I have 2 simple POCO classes. public class
I want to start and learn coding to create a web application. I have
I start to learn AnyEvent and have some trobles with it. I totally misunderstood
I recently start to learn CSS and table less design. After reviewing some tutorials
I have recently start to learn WPF + MVVM. It is possible I have
I'm a little bit confused. I want to start learn Flex3 with Eclipse and
I'm learned php as functional and procedure language. Right now try to start learn
I start to learn class in PHP. According to my experience with other language,
I am looking to start from scratch to learn to program embedded systems. After

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.