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

The Archive Base Latest Questions

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

I have a question about ruby on rails and the process of assigning variables

  • 0

I have a question about ruby on rails and the process of assigning variables using the params variable passed through a form

class User
  attr_accessible :available_to_admins, :name
end

Let’s say that I have a field that is only available to my admins. Assuming that you are not an admin, I am going to not display the available_to_admins input in your form.

After that, when I want to save your data I’ll just do a:

User.update_attributes(params[:user])

If you are an admin, then no problem, the params[:user] is going to contain name and available_tu_admins and if you’re not then only your name.

Since the available_to_admins is an attr_accessible parameter, how should I prevent non admin users from being able to inject a variable containing the available_to_admins input with their new value?

  • 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-14T06:57:01+00:00Added an answer on May 14, 2026 at 6:57 am

    a. You can check user role in controller.

    class User
      # remove available_to_admins from attr_accessible
      attr_accessible :name
    end
    
    def update
      @user = User.new(params[:user])
      @user.available_to_admins = params[:user][:available_to_admins] if current_user.role == 'Admin'
    end
    

    b. You can add before_save / before_update callbacks to your model

    class User
      # remove available_to_admins from attr_accessible
      attr_accessible :name
      before_save :check_role
      before_update :check_role
      def check_role
        self.available_to_admins = params[:user][:available_to_admins] if current_user.role == 'Admin'
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about doing a query through a few associations using Ruby
Hi I have a question about ruby on rails Apparently I have a statement
I'm a Ruby newbie. Have a very basic question about static and instance variables.
I am learning Ruby on Rails and I have a dumb question about the
I'm reading a book about Ruby/Rails and have a question about something simple. In
I have a question about models on Ruby on Rails: I have this method
I’m relatively new to Ruby, Sinatra, and DataMapper, but have a question about DataMapper
I am a new Ruby on Rails user and had a question. I have
I'm just start to develop on Ruby on Rails and i've question about complex
I have a question regarding Ruby on Rails. I'm currently use a gem called

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.