2012-04-06 99 views
0

我已經完成了Michael Hartl Ruby on Rails教程(適用於Rails 3),我想知道如何去顯示所有已分配Admin屬性的用戶,但是在一個單獨的頁面上,因爲它沒有提到這個地方。在Ruby on Rails中顯示管理員用戶索引

users_controller.rb

class UsersController < ApplicationController 
    before_filter :authenticate, :only => [:index, :edit, :update, :destroy] 
    before_filter :correct_user, :only => [:edit, :update] 
    before_filter :admin_user, :only => :destroy 

    def show 
    @user = User.find(params[:id]) 
    @microposts = @user.microposts.paginate(:page => 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 => 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 => params[:page]) 
    render 'show_follow' 
    end 

    def followers 
    @title = "Followers" 
    @user = User.find(params[:id]) 
    @users = @user.followers.paginate(:page => 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 

的routes.rb

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 => [:new, :create, :destroy] 
    resources :microposts, :only => [:create, :destroy] 
    resources :relationships, :only => [:create, :destroy] 
    get "sessions/new" 

    match '/signup', :to => 'users#new' 
    match '/signin', :to => 'sessions#new' 
    match '/signout', :to => 'sessions#destroy' 

    match '/sign_up', :to => 'pages#sign_up' 

    root :to => 'pages#home' 

    resources :users 
    match '/signup', :to => 'users#new' 

end 

user.rb

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :name, :email, :password, :password_confirmation 

    has_many :microposts, :dependent => :destroy 
    has_many :relationships, :foreign_key => "follower_id", :dependent => :destroy 
    has_many :following, :through => :relationships, :source => :followed 
    has_many :reverse_relationships, :foreign_key => "followed_id", :class_name => "Relationship", :dependent => :destroy 
    has_many :followers, :through => :reverse_relationships, :source => :follower 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

    validates :name, :presence => true, :length => { :maximum => 50 } 
    validates :email, :presence => true, :format => { :with => email_regex }, :uniqueness => { :case_sensitive => false } 

    scope :admins, where(:admin => true) 

    # Automatically create the virtual attribute 'password_confirmation'. 
    validates :password, :presence => true, :confirmation => true, :length => { :within => 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 && user.salt == cookie_salt) ? user : nil 
    end 

    def following?(followed) 
    relationships.find_by_followed_id(followed) 
    end 

    def follow!(followed) 
    relationships.create!(:followed_id => 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 

回答

1

你應該做到以下幾點:

UsersController 

def admins 
    @admins=User.where(:admin => true) 
end 

而且它添加到你的路由文件:

resources :users do 
    collection do 
     get :admins 
    end 
end 

然後只需在admins.html.erb視圖中顯示@admins實例變量(您可以在用戶視圖中創建該變量)。

+0

我試過這個,但是它出現了一個錯誤「UsersController#show中的ActiveRecord :: RecordNotFound」,應用程序跟蹤中顯示「app/controllers/users_controller」。RB:7:在 '顯示」' 我試圖更新DEF顯示於以下 'DEF show' '@user = User.find(PARAMS [:ID])'' = @microposts @ user.microposts.paginate(:page => params [:page])' '@title = @ user.name' '@admins = User.find(params [:id])' 'end' – Spoons 2012-04-07 11:54:32

+0

The best開始使用ActiveRecord的方法是使用rails控制檯來獲得對你的查詢的即時反饋。試試'admins = User.where(:admin =>「true」),你會得到一個符合你的標準的所有對象的數組。至於你的管理員實例變量,它應該是@ admins = User.where(:admin => true)而不是@admins = User.find(params [:id]) – 2012-04-07 13:17:43

2

首先,竟被你d需要在您的config/routes.rb文件中創建一條路線,以便路由到可顯示此信息的操作。事情是這樣的:

resources :users do 
    collection do 
    get :admins 
    end 
end 

那倒路線UsersControlleradmins動作,所以這就是你需要下一個定義什麼。它會是這樣的:

def admins 
    @users = User.admins 
    render "users/index" 
end 

因爲管理員列表中不應該從用戶列表太不一樣了,你可以分配所有管理員能夠@users,然後渲染users/index模板......如果它存在。我可能在這裏假設太多,但這是一種做法。

現在,您的User類中沒有admins方法,因此您需要定義該方法。要做到這一點的方法之一是使用範圍,就像這樣:

scope :admins, where(:admin => true) 

這將確定在Useradmins方法,返回誰是管理員的所有用戶的範圍。範圍很酷,你應該看看他們能做些什麼。

或者,你可以定義一個類的方法:

def self.admins 
    where(:admin => true) 
end 
+0

我有點困惑,我已經完成了上面推薦的所有內容,並且我在app/views/users中有文件index.html.erb。我嘗試添加<%= render @admins%>,但它在用戶#索引中產生了NoMethodError,所以我將其刪除。 – Spoons 2012-04-06 18:30:05

+0

你爲什麼要把'<%= render @admins%>'?您將其分配給@users變量並引用現有的用戶#索引模板。你應該可以打電話給/用戶/管理員,並讓它工作。 – davidcelis 2012-04-06 19:02:26

相關問題