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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:34:54+00:00 2026-05-17T02:34:54+00:00

I’m trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using

  • 0

I’m trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using roles like Ryan B’s screencast and other examples around I’m trying to do something basic:

1 – A user can log in
2 – A user can only edit/destroy their own articles (no roles, you’re either logged in and can create new articles and edit/destroy your own or you’re logged out and you can only see articles and login)

I’m using devise for the first part and that’s working well but I can’t get the second part working with CanCan. The the edit and destroy links for the articles don’t appear when you’re logged in and the direct URL (e.g. /articles/3/edit) still allows even if the article is for another user.

My ability.rb is

class Ability
  include CanCan::Ability

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

    if user.nil? 
      can :read, :all
    else
#      can :manage, :all #test - with this, all the edit/destroy links appear
       can :manage, Article, :user_id == user
    end
  end
end

articles_controller.rb:

class ArticlesController < ApplicationController

  before_filter :authenticate_user!, :except => [:index, :show] # for Devise
  load_and_authorize_resource


  # GET /articles
  # GET /articles.xml
  def index

    @articles = Article.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @articles }
    end
  end

  # GET /articles/1
  # GET /articles/1.xml
  def show
    @article = Article.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/new
  # GET /articles/new.xml
  def new
    @article = Article.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/1/edit
  def edit
    @article = Article.find(params[:id])
  end

  # POST /articles
  # POST /articles.xml
  def create
    @article = Article.new(params[:article])
    @article.user = current_user

    respond_to do |format|
      if @article.save
        format.html { redirect_to(articles_path, :notice => 'Article was successfully created.') }
        format.xml  { render :xml => articles_path, :status => :created, :location => articles_path }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /articles/1
  # PUT /articles/1.xml
  def update
    @article = Article.find(params[:id])

    respond_to do |format|
      if @article.update_attributes(params[:article])
        format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.xml
  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    respond_to do |format|
      format.html { redirect_to(articles_url) }
      format.xml  { head :ok }
    end
  end
end

and the view partial that lists articles _article_list.html.erb:

    <table>
      <tr>
        <th>Title</th>
        <th>Description</th>
        <th>User</th>
        <th></th>
        <th></th>
        <th></th>
      </tr>

    <% @articles.each do |article| %>
      <tr>
        <td><%= article.title %></td>
        <td><%= article.description %></td>
        <td><%= article.user_id %></td>
        <td><%= link_to 'Show', article %></td>
        <% if can? :update, @article %>
            <td><%= link_to 'Edit', edit_article_path(article) %></td>
        <% end %>
        <% if can? :destroy, @article %>
            <td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
        <% end%>
      </tr>
    <% end %>
    </table>

With this setup, the edit/destroy links in the view don’t show up unless there’s a blanket can :manage, :all, even can :manage, Article doesn’t work. As I mentioned above, it also isn’t restricting the actual actions as you’re able to deep link straight to editing an article and it permits it.

I’m not sure what I’m doing wrong here. It would be great to get some help.

Thanks in advance
Jason

  • 1 1 Answer
  • 1 View
  • 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-17T02:34:55+00:00Added an answer on May 17, 2026 at 2:34 am

    I managed to resolve my problem. I reset my environment (rvm – resintalled the gems and gemsets – ruby 1.9.2 and rails 3.0.0) and changed some of the code and all the issues I was having went away (redirect loop, view elements not changing based on being logged in, unauthorized controller actions still permissable). I’ve pasted ability.rb, articles_controller.rb, and _article_list.html.erb.

    ability.rb:

    class Ability
      include CanCan::Ability
    
      def initialize(user)
        if user
          can :create, Article
          can :read, :all
          can :update, Article, :user_id => user.id
          can :delete, Article, :user_id => user.id
        else
          can :read, :all
        end
      end
    end
    

    I guess it makes sense now but because only update and delete were supposed to be for the current user’s articles, I split out the CRUD elements to be specific.

    articles_controller.rb

    class ArticlesController < ApplicationController
    
      before_filter :authenticate_user!, :except => [:index, :show]
    #  load_and_authorize_resource # RESTful automated CanCam authorization - excludes non RESTful
    
      # GET /articles
      # GET /articles.xml
      def index
        @articles = Article.all
        authorize! :read, @articles
    
    
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @articles }
        end
      end
    
      # GET /articles/1
      # GET /articles/1.xml
      def show
        @article = Article.find(params[:id])
        authorize! :read, @article
    
        respond_to do |format|
          format.html # show.html.erb
          format.xml  { render :xml => @article }
        end
      end
    
      # GET /articles/new
      # GET /articles/new.xml
      def new
        @article = Article.new
        authorize! :create, @article
    
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @article }
        end
      end
    
      # GET /articles/1/edit
      def edit
        @article = Article.find(params[:id])
        authorize! :update, @article
      end
    
      # POST /articles
      # POST /articles.xml
      def create
        @article = Article.new(params[:article])
        @article.user = current_user
        authorize! :create, @article
    
        respond_to do |format|
          if @article.save
            format.html { redirect_to(articles_path, :notice => 'Article was successfully created.') }
            format.xml  { render :xml => articles_path, :status => :created, :location => articles_path }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
          end
        end
      end
    
      # PUT /articles/1
      # PUT /articles/1.xml
      def update
        @article = Article.find(params[:id])
        authorize! :update, @article
    
        respond_to do |format|
          if @article.update_attributes(params[:article])
            format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
          end
        end
      end
    
      # DELETE /articles/1
      # DELETE /articles/1.xml
      def destroy
        @article = Article.find(params[:id])
        @article.destroy
        authorize! :delete, @article
    
        respond_to do |format|
          format.html { redirect_to(articles_url) }
          format.xml  { head :ok }
        end
      end
    
      def by
        @user = User.find(params[:id])
        @articles = @user.articles
        authorize! :read, @articles
      end
    end
    

    load_and_authorize_resource works but I’ve put specific authorize! lines in each controller action as I have an extra action at the bottom. Both now work.

    I updated the reference to @article to article to reference the current article in the list in _article_list.html.rb:

    <table>
      <tr>
        <th>Title</th>
        <th>Description</th>
        <th>User</th>
        <th></th>
        <th></th>
        <th></th>
      </tr>
    
    <% @articles.each do |article| %>
      <tr>
        <td><%= article.title %></td>
        <td><%= article.description %></td>
        <td><%= article.user_id %></td>
        <td><%= link_to 'Show', article %></td>
        <% if can? :update, article %>
            <td><%= link_to 'Edit', edit_article_path(article) %></td>
        <% end %>
        <% if can? :delete, article %>
            <td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
        <% end %>
      </tr>
    <% end %>
    </table>
    

    All working now. Thanks for the help here and hopefully this will help someone else out if they run into this problem.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.