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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:33:12+00:00 2026-05-24T12:33:12+00:00

Trying to get Cancan securing a few models in an application and curious why

  • 0

Trying to get Cancan securing a few models in an application and curious why it’s not working the way I thought it would. I had thought you could can? on the specific instance as opposed to the entire class so, not in this example but, you could enable abilities on a per instance basis as a list of posts are displayed?!?

class Ability
  include CanCan::Ability
  def initialize(user) 
    user ||= User.new # guest user (not logged in)
    if user.role? :admin
      can :manage, :all
    elsif user.role? :moderator
      can :manage, Post
    else
      can :read, :all
    end
  end
end

# posts/index.html.haml
...
- if can? :update, @post   <- doesn't work
- if can? :update, Post    <- works

Edit: add PostsController.rb

#posts_controller.rb
class PostsController < ApplicationController
  before_filter :login_required, :except => [:index, :show]
  load_and_authorize_resource :except => [:create]

  def index
    # @posts = Post.all ## <- handled by Cancan's load_and_authorize_resource
    @events = Event.where("end_date <= :today", :today => Date.today)
    @next_event = Event.next

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
  ...
end
  • 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-24T12:33:13+00:00Added an answer on May 24, 2026 at 12:33 pm

    This line:

    - if can? :update, @post   <- doesn't work
    

    Is asking CanCan “can I update this specific post.” You defined the ability in terms of all posts. If you had done:

    can :update, Post, :user_id => user.id
    

    Then your “if can?” would work, and the user would only be able to update their own posts. So you want to use the specific resource version (“@post”) if something about this instance of the resource determines the permission, and you want to use the class version (“Post”) if the user has the ability for all instances of the class.

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

Sidebar

Related Questions

Trying to get an ASP application deployed; it worked for a while but then
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
I am trying get content from the link https://....com:8455 where Server's certificate is not
Hello everyone I been trying get php uploader working but having a lot of
I am trying to get cancan to work with a polymorphic association, i have
I am using cancan and trying to get it to limit equipment shown for
I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
Trying to get a value from a object isn't working out. class Asset <
Trying to get a login script working, I kept getting the same login page
Im trying get all my maingroups with its related entities. And i would also

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.