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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:40:19+00:00 2026-05-28T03:40:19+00:00

How would you prevent other users from editing a object, say a profile object

  • 0

How would you prevent other users from editing a object, say a profile object that does – not – belong to themselves?

Most online examples are complexes with multiple user roles, i haven’t been able to get this working, must be simple though:

  def initialize(user)

      can :update, Profile do |profile|
        profile.try(:user) == current_user
      end

  end

And inside my ProfilesController#edit

authorize! :update, @profile
  • 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-28T03:40:19+00:00Added an answer on May 28, 2026 at 3:40 am

    First question is, have you made your roles for the User?

    app/models/user.rb

    class User < ActiveRecord::Base
      attr_accessible :email, :password, :remember_me
      devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, # regular devise stuff
      before_create :setup_default_role_for_new_users
    
      ROLES = %w[admin default banned]
    
      private
    
      def setup_default_role_for_new_users
        if self.role.blank?
          self.role = "default"
        end
      end
    end
    

    As you can see I have 3 different roles here and when a new user is created they are always default users. Now with CanCan set up, lets say you wanted to have the admin be able to do everything, the default users be able to do everything with their own profiles, banned users cannot do anything and guest users be able to see profiles:

    class Ability
      include CanCan::Ability
      # Remember that CanCan is for a resource, meaning it must have a class(model).
    
      def initialize(user)
        user ||= User.new # guest user (not logged in)
    
        if user.role == "admin"
          can :manage, :all
        elsif user.role == "default"
          can :manage, Profile, :user_id => user.id
        elsif user.role == "banned"
          cannot :manage, :all
        else
          can :read, Profile # guest user
        end
      end
    end
    

    So that’s how you let users edit only their own profiles and nobody elses.


    Some other handy notes: Make sure you have a user_id column in your Profile table. Also if you may need to let guess users see profiles like this:

    class ProfileController < ApplicationController
        before_filter :authenticate_user!, :except => :show
        load_and_authorize_resource
    end
    

    They won’t be able to use any other action and CanCan still checks authentication on everything else except show.

    Good luck!


    UPDATE: Making :role attribute for Users

    What I did was run a migration that would add the role column to the Devise users table:

    rails generate migration add_role_to_users role:string
    

    And then rake db:migrate. The new migration file should look like this and also check your db/schema.rb file to make sure its apart of the users table correctly. If it isn’t then rake db:drop, then rake db:create and then rake db:migrate again.

    class AddRoleToUsers < ActiveRecord::Migration
      def self.up
        add_column :users, :role, :string
      end
    
      def self.down
        remove_column :users, :role
      end
    end
    

    This is how you successfully make the user.role work.

    Note: Make sure you leave the line: can :manage, Profile, :user_id => user.id as is with no changes. It should work after adding the role column to user.

    IMPORTANT! If you use Rails 3, DO NOT MAKE role attr_accessible or everyone can edit their roles! Rails 4 uses Strong Parameters by default and is not affected by this issue as you can choose the allowed parameters.

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

Sidebar

Related Questions

Are there any compatibility issues that would prevent this from working? Do I need
It seems that licensing terms would prevent us from using Google Maps API in
I thought by placing these two lines that it would prevent the page from
Does cygwin allow a statically compiled binary? This would prevent the need for cygwin1.dll
I would like to prevent duplicate content. I do not want to keep a
I would like to prevent a property from being exposed via my WCF web
If you have some blocks of code that you would like to prevent execution
I would like to restrict users from inserting more than 3 records with color
Is it possible to prevent someone other than those allowed from tampering with the
I would like to have group based restrictions that would allow users to access

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.