Ruby on rails 在RubyonRails中显示管理员用户的索引

Ruby on rails 在RubyonRails中显示管理员用户的索引,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我已经完成了Michael Hartl Ruby on Rails教程(适用于Rails 3),我想知道您将如何在一个单独的页面上显示所有被分配了Admin属性的用户,因为它在任何地方都没有提到这一点 用户\u控制器.rb class UsersController < ApplicationController before_filter :authenticate, :only => [:index, :edit, :update, :destroy] befor

我已经完成了Michael Hartl Ruby on Rails教程(适用于Rails 3),我想知道您将如何在一个单独的页面上显示所有被分配了Admin属性的用户,因为它在任何地方都没有提到这一点

用户\u控制器.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
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
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+\-.]+@[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
user.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
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
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+\-.]+@[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
class用户:销毁
有很多:关系,:外键=>“跟随者id”,:依赖=>:销毁
有很多:following,:through=>:relationships,:source=>:following
有很多:反向关系,:外部键=>“跟随的\u id”,:类名称=>“关系”,:依赖=>:销毁
拥有多个:追随者,:通过=>:反向关系,:源=>:追随者
电子邮件\u regex=/\A[\w+\-.]+@[A-z\d\-.]+\[A-z]+\z/i
验证:name,:presence=>true,:length=>{:max=>50}
验证:email,:presence=>true,:format=>{:with=>email\u regex},:university=>{:case\u sensitive=>false}
作用域:admins,其中(:admin=>true)
#自动创建虚拟属性“密码确认”。
验证:password,:presence=>true,:confirmation=>true,:length=>{:within=>6..40}
保存前:加密密码
def有密码?(已提交密码)
加密密码==加密(已提交密码)
结束
def自我验证(电子邮件、提交的密码)
用户=通过电子邮件查找(电子邮件)
如果user.nil,则返回nil?
如果用户有密码,则返回用户?(已提交密码)
结束
def自我验证使用_盐(id,cookie_盐)
用户=通过\u id(id)查找\u
(user&&user.salt==cookie\u salt)?用户:无
结束
def跟随?(跟随)
关系。按\u查找\u,后跟\u id(后跟)
结束
跟我来!(随后)
关系。创造!(:followed_id=>followed.id)
结束
放轻松!(随后)
关系。按查找查找,然后按id查找。销毁
结束
def供给
来自用户的微成本,然后是(自身)
结束
私有的
def加密密码
self.salt=make_salt,除非有密码?(密码)
self.encrypted_password=加密(密码)
结束
def加密(字符串)
安全散列(“#{salt}--#{string}”)
结束
def制盐
安全散列(“#{Time.now.utc}--#{password}”)
结束
def secure_散列(字符串)
摘要::SHA2.hexdigest(字符串)
结束
结束

首先,您需要在
配置/routes.rb
文件中创建一个路由,该路由将路由到显示此信息的操作。大概是这样的:

resources :users do
  collection do
    get :admins
  end
end
def admins
  @users = User.admins
  render "users/index"
end
scope :admins, where(:admin => true)
这将路由到
userscoontroller
中的
admins
操作,因此这就是您接下来需要定义的内容。它是这样的:

resources :users do
  collection do
    get :admins
  end
end
def admins
  @users = User.admins
  render "users/index"
end
scope :admins, where(:admin => true)
因为管理员列表不应该与用户列表有太大的不同,所以您可以将所有管理员分配给
@users
,然后呈现
用户/索引
模板。。。如果存在的话。我可能在这里假设得太多了,但这是一种方法

现在,在
User
类上没有
admins
方法,因此您需要定义它。一种方法是使用范围,如下所示:

resources :users do
  collection do
    get :admins
  end
end
def admins
  @users = User.admins
  render "users/index"
end
scope :admins, where(:admin => true)
这将在
User
类上定义
admins
方法,返回所有管理员用户的范围。示波器很酷,你应该看看它们还能做什么

或者,您可以定义一个类方法:

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

首先,您需要在
config/routes.rb
文件中创建一个路由,该路由将路由到显示此信息的操作。大概是这样的:

resources :users do
  collection do
    get :admins
  end
end
def admins
  @users = User.admins
  render "users/index"
end
scope :admins, where(:admin => true)
这将路由到
userscoontroller
中的
admins
操作,因此这就是您接下来需要定义的内容。它是这样的:

resources :users do
  collection do
    get :admins
  end
end
def admins
  @users = User.admins
  render "users/index"
end
scope :admins, where(:admin => true)
因为管理员列表不应该与用户列表有太大的不同,所以您可以将所有管理员分配给
@users
,然后呈现
用户/索引
模板。。。如果存在的话。我可能在这里假设得太多了,但这是一种方法

现在,在
User
类上没有
admins
方法,因此您需要定义它。一种方法是使用范围,如下所示:

resources :users do
  collection do
    get :admins
  end
end
def admins
  @users = User.admins
  render "users/index"
end
scope :admins, where(:admin => true)
这将在
User
类上定义
admins
方法,返回所有管理员用户的范围。示波器很酷,你应该看看它们还能做什么

或者,您可以定义一个类方法:

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

您应该执行以下操作:

UsersController

def admins
   @admins=User.where(:admin => true)
end
并将其添加到路由文件中:

 resources :users do
    collection do
      get :admins
    end
 end

然后在admins.html.erb视图中呈现@admins实例变量(您将在用户视图中创建该视图)。

您应该执行以下操作:

UsersController

def admins
   @admins=User.where(:admin => true)
end
并将其添加到路由文件中:

 resources :users do
    collection do
      get :admins
    end
 end

然后在admins.html.erb视图中呈现@admins实例变量(您将在用户视图中创建该变量)。

我有点困惑,我已经完成了上面建议的所有操作,并且在app/views/users中有index.html.erb文件。我尝试过添加,但它在用户索引中产生了一个命名错误,所以我删除了它。为什么要添加
?您正在将其分配给@users变量并引用现有的users#索引模板。你应该可以打电话给/users/admins,让它工作。我有点困惑,我已经完成了上面你推荐的所有操作,并且我在app/views/users中有index.html.erb文件。我尝试过添加,但它在用户索引中产生了一个命名错误,所以我删除了它。为什么要添加
?您正在将其分配给@users变量并引用现有的users#索引模板。你应该能够调用/users/admins并让它工作。我已经尝试过了,但是出现了一个错误“ActiveRecord::RecordNotFound in UsersController#show”,应用程序跟踪显示“app/controllers/u”