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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:23:11+00:00 2026-06-12T01:23:11+00:00

In my app there’s a user model with an admin role. I want admins

  • 0

In my app there’s a user model with an admin role. I want admins to be able to create new admins, so on the users/edit page I’m hoping to create an admin-visible checkbox that, when checked turns the user into an admin. The code I’m using is here. Everything works other than the checkbox bit, which is why I’m asking. I’m new to Rails, and I just can’t figure out how to scrape the admin checkbox info and use it to alter the admin attribute (which is attr_protected for security reasons).

My edit form

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
    <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <% if signed_in? %>
        <% if current_user.admin? %>
          <h3> Click to Switch On/Off Admin  
          <%= check_box_tag(:admin) %></h3>
        <% end %>
      <% end %>

      <%= f.submit @string, class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

The relevant lines of my user.rb file

before_save :toggle_admin

private

    def toggle_admin
      if self.admin == true
        self.toggle!(:admin)
      end
    end

What am I doing wrong? It doesn’t toggle anything when I submit.

Thanks!

Sasha

  • 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-12T01:23:12+00:00Added an answer on June 12, 2026 at 1:23 am

    Actually problem is because you had added :admin checkbox incorrectly. You have to do it like this:

    <%= f.check_box :admin %>
    

    It will produce input with correct name:

    <input type="checkbox" id="user_admin" name="user[admin]" value="0" />
    

    So then you will have correct params hash which is sent after form submission.

    For additional details please check Rails documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box

    Update #1

    If in your case you want to use toggle for security reasons, just change before_save to after_save, because toggle works like this:

    User.last.toggle!(:admin)
    

    SQL output:

    1.9.3p194 :007 > User.last.toggle!(:admin)
      User Load (0.6ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
      SQL (83.8ms)  UPDATE `users` SET `admin` = 1 WHERE `users`.`id` = 13
     => true 
    
    1.9.3p194 :008 > User.last.toggle!(:admin)
      User Load (0.6ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
      SQL (76.7ms)  UPDATE `users` SET `admin` = 0 WHERE `users`.`id` = 13
    

    Update #2

    In principle, you can implement it using a virtual attribute for checkbox.

    after_save :toggle_admin
    attr_accessor :adminify
    attr_accessible :adminify
    
    private
    
        def toggle_admin
          if self.adminify == "1"
            self.toggle!(:admin)
          end
        end
    

    Check tutorial about virtual attributes in Rails: http://railscasts.com/episodes/167-more-on-virtual-attributes

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

Sidebar

Related Questions

In my flex app there are a few custom components. I want to create
In my app there is a webview with an advertising html page in it.
In my app there is a button, user just click it then the latest
When hosting WPF user controls within a WinForms MDI app there is a drawing
I have a simple checkin app: there's a page with a series of pictures
In my app,there is a gmail login section where user can login to the
In my GAE app there is the following line : ContactsService myService=new ContactsService(app-name); It
For a page in our app there is a certain number of customers that
In my app there are 2 edit text, one for username and other for
Helllo all, in my app there is a story 1 hour i want to

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.