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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:54:03+00:00 2026-06-15T22:54:03+00:00

In my User model, I have this: attr_accessible :role_ids, :as => :admin But that

  • 0

In my User model, I have this:

attr_accessible :role_ids, :as => :admin

But that doesn’t seem to work.

I want this particular attribute to be only accessible if the current_user.can?(:edit, Role) – i.e. only users with a role of admin or superadmin should be able to access those attributes.

How do I do this?

Edit 1: I am using Devise, CanCan & Rolify.

  • 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-15T22:54:04+00:00Added an answer on June 15, 2026 at 10:54 pm

    Like I said, we think the best way to restrict attributes to certain roles is to use the strong parameters gem that DHH introduced recently. This will also be part of Rails4 thankfully.

    Even if it doesn’t fit, it’s a good idea to start integrating the principles as it will make your Rails 3 to 4 upgrade easier.

    If you have a Railscasts Pro membership, Ryan Bates has made another fantastic tutorial on it.

    In brief, here’s what’s recommended in that Railscast.

    After installing the gem, remove attr_accessible from your model.

    Add this to an initializer:

    ActiveRecord::Base.send(:include,  ActiveModel::ForbiddenAttributesProtection)
    

    Alter your update action in your controller::

    def update
      @topic = Topic.find(params[:id])
      if @topic.update_attributes(topic_params)
        redirect_to topics_url, notice: "Updated topic."
      else
        render :edit
      end
    end 
    

    Create a private method in your controller:

     def topic_params
       if current_user && current_user.admin?
         params[:topic].permit(:name, :sticky)
        else
          params[:topic].permit(:name)
        end
      end
    

    In your situation, like we do, you’d have to change the topic_params method to use your roles.

    There’s a few more suggestions in the RailsCast and it’s really worth the $9! (I’m in no way affiliated with the site, it’s just proven invaluable to us)

    Let me know if this helps.

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

Sidebar

Related Questions

I have a Rails app with a user model that contains an admin attribute.
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have a model that holds user address. This model has to have first_name
I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,
I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model.py like this: class EventTypeCategory(models.Model): name = models.CharField(max_length=50, verbose_name=Name) user =
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
So I have the basics setup already. This is my User model: # ==

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.