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

  • Home
  • SEARCH
  • 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 5927823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:13:24+00:00 2026-05-22T14:13:24+00:00

I am using Devise for authentication, and I only need a simple admin or

  • 0

I am using Devise for authentication, and I only need a simple admin or use check for a few controllers. I’m new to rails, so I’m trying to do this the right way. I’ve basically added a boolean admin field to the user model and added this method

  def is_admin?
    admin == 1
  end

Then I simply modified the controller action to this

  def new
    if current_user.nil? || !current_user.is_admin?
        flash[:notice] = "You do not have permission to view this page"
        redirect_to "/gyms"
      else
      @gym = Gym.new

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

So this solution works, but should I be doing this a different way?

  • 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-22T14:13:25+00:00Added an answer on May 22, 2026 at 2:13 pm

    This will work but I probably would not recommend this solution for anything else than a small scale project. Over time, if you perform authorization checks within your controllers, your code is going to become bloated and difficult to manage.

    Instead I would consider using an authorization module such as Cancan which centralizes your authorization rules in one place and thus decouples your application logic from your authorization logic. The end result is cleaner and more maintainable code.

    With Cancan in place, your code might look like this:

    # app/controllers/gyms_controller.rb
    class GymsController < ApplicationController
      load_and_autorize_resource
    
      def new
          respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @gym }
          end
        end
      end
    end
    
    # app/models/Ability.rb
    can :create, Gym do |trip|
      user.is_admin?
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get some basic authentication/authorization with devise/cancan with Rails. Rather than using
I'm trying to layout a Rails app using Devise for authentication. I'd like to
I'm using Devise for authentication in my Rails app. I'd like to eager load
I have an app that is using devise for authentication. Rails 3 on ruby
I am using Devise for authentication, so I've aliased a few columns in my
I'm using the devise authentication plugin under rails 3. At the moment, I have
I am using Devise for authentication, so I've aliased a few columns in my
i'm currently buidling my own blog using rails 3. and use devise gem for
I'm using rails 2.3.5 and devise 1.0.6. I'm having users confirm account's with email.
I'm working on a Ruby on Rails application that used the Devise authentication system.

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.