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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:35:57+00:00 2026-05-19T06:35:57+00:00

i don’t quite understand how to restrict access to links in this particular case

  • 0

i don’t quite understand how to restrict access to links in this particular case with CanCan. I always get “Edit” link displayed.
So i believe the problem is in my incorrect definition of cancan methods(load_ and authorize_).
I have CommentsController like that:

class CommentsController < ApplicationController
  before_filter :authenticate_user!
  load_resource :instance_name => :commentable
  authorize_resource :article
  def index
    @commentable = find_commentable #loading our generic object
  end

......

  private

  def find_commentable               
    params.each { |name, value|
      if name =~ /(.+)_id$/
        return $1.classify.constantize.includes(:comments => :karma).find(value)
      end }
  end
end

and i have in comments/index.html.erb following code that render file from other controller:

<%= render :file => "#{get_commentable_partial_name(@commentable)}/show.html.erb", :collection => @commentable %>

you can think about “#{get_commentable_partial_name(@commentable)}” like just “articles” in this case.
Content of “articles/show.html.erb”:

<% if can? :update, @commentable %>
    <%= link_to 'Edit', edit_article_path(@commentable) %> |
<% end %>

my ability.rb:

class Ability
  include CanCan::Ability

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

    if user.role? :admin
      can :manage, :all
    elsif user.role? :author
        can :read, [Article, Comment, Profile]
        can :update, Article, :user_id => user.id
    end
  end
end

i have tried debug this issue like that

user = User.first
article = Article.first
ability = Ability.new(user)
ability.can?(:update, article)

and i always get “=> true” in ability check

Note: user.role == author and article.user_id != user.id

if you need more information please write

thank’s for your time && sorry for my english

  • 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-19T06:35:59+00:00Added an answer on May 19, 2026 at 6:35 am

    okay i figure it out, redetermined rules in ability.rb so now order is like guest->author->moderator->admin and problem is solved. I believe root of problem was in cancan logic which assumes that i need to redefine rules or do it in order i’ve show before

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

Sidebar

Related Questions

Don't quite understand why this copy constructor is not invoked when I build with
Don't understand this simple code: def main(): print (This program illustrates a chaotic function)
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't get me wrong, I want them to get saved. But I always thought
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I don't know why, but this code worked for me a month ago... maybe
Don't these two mean the same thing, first get the value and then increment?
Don't think that I'm mad, I understand how php works! That being said. I
I don't know how better to describe this. So take this example. var a

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.