Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay an Index of Admin Users in Ruby on Rails
    primarykey
    data
    text
    <p>I've completed the Michael Hartl Ruby on Rails Tutorial (for Rails 3) and I am wondering how you would go about displaying all the users that have been assigned the Admin attribute but on a separate page as it doesn't mention this anywhere.</p> <p><strong>users_controller.rb</strong></p> <pre><code> class UsersController &lt; ApplicationController before_filter :authenticate, :only =&gt; [:index, :edit, :update, :destroy] before_filter :correct_user, :only =&gt; [:edit, :update] before_filter :admin_user, :only =&gt; :destroy def show @user = User.find(params[:id]) @microposts = @user.microposts.paginate(:page =&gt; params[:page]) @title = @user.name end def new @user = User.new @title = "Sign up" end def create @user = User.new(params[:user]) if @user.save sign_in @user flash[:success] = "Welcome to University Sports!" redirect_to @user else @title = "Sign up" render 'new' end end def edit @title = "Edit user" end def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) flash[:success] = "Profile updated." redirect_to @user else @title = "Edit user" render 'edit' end end def index @users = User.paginate(:page =&gt; params[:page]) end def admins @users = User.admins render "users/index" end def destroy User.find(params[:id]).destroy flash[:success] = "User destroyed." redirect_to users_path end def following @title = "Following" @user = User.find(params[:id]) @users = @user.following.paginate(:page =&gt; params[:page]) render 'show_follow' end def followers @title = "Followers" @user = User.find(params[:id]) @users = @user.followers.paginate(:page =&gt; params[:page]) render 'show_follow' end private def authenticate deny_access unless signed_in? end def correct_user @user = User.find(params[:id]) redirect_to(root_path) unless current_user?(@user) end def admin_user redirect_to(root_path) unless current_user.admin? end end </code></pre> <p><strong>Routes.rb</strong></p> <pre><code>FinalProject::Application.routes.draw do get "club/new" resources :users do member do get :following, :followers end end resources :users do collection do get :admins end end resources :sessions, :only =&gt; [:new, :create, :destroy] resources :microposts, :only =&gt; [:create, :destroy] resources :relationships, :only =&gt; [:create, :destroy] get "sessions/new" match '/signup', :to =&gt; 'users#new' match '/signin', :to =&gt; 'sessions#new' match '/signout', :to =&gt; 'sessions#destroy' match '/sign_up', :to =&gt; 'pages#sign_up' root :to =&gt; 'pages#home' resources :users match '/signup', :to =&gt; 'users#new' end </code></pre> <p><strong>user.rb</strong></p> <pre><code>class User &lt; ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation has_many :microposts, :dependent =&gt; :destroy has_many :relationships, :foreign_key =&gt; "follower_id", :dependent =&gt; :destroy has_many :following, :through =&gt; :relationships, :source =&gt; :followed has_many :reverse_relationships, :foreign_key =&gt; "followed_id", :class_name =&gt; "Relationship", :dependent =&gt; :destroy has_many :followers, :through =&gt; :reverse_relationships, :source =&gt; :follower email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :name, :presence =&gt; true, :length =&gt; { :maximum =&gt; 50 } validates :email, :presence =&gt; true, :format =&gt; { :with =&gt; email_regex }, :uniqueness =&gt; { :case_sensitive =&gt; false } scope :admins, where(:admin =&gt; true) # Automatically create the virtual attribute 'password_confirmation'. validates :password, :presence =&gt; true, :confirmation =&gt; true, :length =&gt; { :within =&gt; 6..40 } before_save :encrypt_password def has_password?(submitted_password) encrypted_password == encrypt(submitted_password) end def self.authenticate(email, submitted_password) user = find_by_email(email) return nil if user.nil? return user if user.has_password?(submitted_password) end def self.authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user &amp;&amp; user.salt == cookie_salt) ? user : nil end def following?(followed) relationships.find_by_followed_id(followed) end def follow!(followed) relationships.create!(:followed_id =&gt; followed.id) end def unfollow!(followed) relationships.find_by_followed_id(followed).destroy end def feed Micropost.from_users_followed_by(self) end private def encrypt_password self.salt = make_salt unless has_password?(password) self.encrypted_password = encrypt(password) end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload