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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:51:07+00:00 2026-05-21T05:51:07+00:00

I was following the tutorial at http://www.logansbailey.com/ and modified it to enable an unregistered

  • 0

I was following the tutorial at http://www.logansbailey.com/ and modified it to enable an unregistered person to be able to register with a username, email and password.
I already enabled a logged in user to modify his/her email and password but not the username.

What I want to add is:

1) to enable a logged in user to be able to see/reach his/her username and email,
2) to enable a user with admin_flag set (I handled this in the sql table and created the user) to be able to see/modify all user records.

I modifyed the app/cotrollers/user_controller.rb like this:

class UsersController < ApplicationController

  before_filter :is_user, :only => [:index, :show, :edit, :update, :destroy]

  def index
    @users = User.all

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

  def show
    @user = User.find(params[:id])

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

  def new
    @user = User.new

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

  def edit
  end

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        flash[:notice] = 'Registration successful.'
        format.html { redirect_to(:controller => 'home', :action => 'tutorial') }
        format.xml { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update

    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = 'Your profile was successfully updated.'
        format.html { redirect_to(:controller => 'home', :action => 'index') }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml { head :ok }
    end
  end

  def is_user
    if User.exists?(params[:id])
      @user = User.find(params[:id]);
      if current_user.admin_flag == true
        flash[:notice] = 'Welcome Admin'
      end
      if !current_user || current_user.id != @user.id
        flash[:notice] = 'You do not have access to that page'
        redirect_to(:controller => 'home', :action => 'index')
      end
    else
      flash[:notice] = 'You do not have access to that page'
      redirect_to(:controller => 'home', :action => 'index')
    end
  end
end

The file app/models/user.rb is:

class User < ActiveRecord::Base
  acts_as_authentic
end

And I can confirm that the admin_flag set user is get correctly since the file app/views/layouts/application.html.erb containing:

  <div id="admin">
    <% if current_user %>
      <% if current_user.admin_flag == true %> |
      <%= link_to "Users", users_path %>
      <% end %>
    <% end %>
  </div>

correctly displays the ‘Users’ link when I log in as the admin.

Now the problem is that I can’t get the show all users, edit other users etc.. functionality. As the admin, I can show and modify the admin user just like all the other ordinary users, meaning I can’t modify the username, too.

What may be wrong here?

  • 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-21T05:51:08+00:00Added an answer on May 21, 2026 at 5:51 am

    When you added a boolean attribute admin to user in the right way, the Rails should add question-mark method admin? in User model. It’s not important but for convenience.
    On every method you want to protect from unwanted actions use before_filter:

    class UsersController < ApplicationController
    
      before_filter :admin_user,   :only => :destroy
      before_filter :correct_user, :only => [:edit, :update]
    
      def destroy
      end
    
      ...
    
      private
    
      def admin_user
        redirect_to(root_path) unless current_user.admin?
      end
    
      def correct_user
        @user = User.find(params[:id])
        redirect_to(root_path) unless current_user?(@user) || current_user.admin?
      end
    end
    

    In views more convenient to use current_user.admin?

    <div id="admin">
      <% if current_user.admin? %>
        <%= link_to "Users", users_path %>
      <% end %>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the following tutorial ( http://www.highoncoding.com/Articles/642_Creating_a_Stock_Widget_in_ASP_NET_MVC_Application.aspx ) on using ajax to render
Trying the following tutorial http://www.androidsdkforum.com/android-sdk-development/3-oauth-twitter.html i am having trouble understanding callback URL my twitter
I have gone through the following tutorial : http://www.javaworld.com/community/node/2915 And after reading the above
I am following this tutorial http://www.codeproject.com/KB/cpp/authforwebservices.aspx They have this in the tutorial [SoapHeader(Authentication, Required
I am following the tutorial http://www.bit-101.com/blog/?p=2115 . In this tutorial I found a project
I was following this tutorial: http://www.marcofolio.net/webdesign/a_fancy_apple.com-style_search_suggestion.html And checking out the demo here: http://qpoit.com/marcofolio_demo/apple_search/ I
I'm following this tutorial: http://www.javascripttoolbox.com/jquery/ And I'm trying to get table rows that are
I'm using Ubuntu 10, python 2.6.5 I'm following this tutorial: http://www.djangobook.com/en/2.0/chapter02 I followed all
right now I'm following an Matlab tutorial http://www.mathworks.com/help/techdoc/creating_guis/brpat2g.html . The Problem is my Matlab
I'm following this tutorial on Flash Pro CS4: http://www.baycongroup.com/flashCS4/09_flashCS4.html I have a button. I

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.