English 中文(简体)
显示铁路局(铁路局)Rub的阿明用户指数
原标题:Trying to display Display an Index of Admin Users in Ruby on RailsError

I ve completed the Michael Hartl Ruby on Rails Tutorial (for Rails 3)

I m trying to get a list of all the admin users on a page at localhost:3000/users/admins however I m getting an error when I go to the page admins. I also have a list of all users under the page localhost:3000/users
The error I m getting is the following:

ActiveRecord::RecordNotFound in UsersController#show
Couldn t find user with id=admins
app/controllers/users_controller.rb:7:in show
Parameters:
{"id"=>"admins"}

在铁路中,能够打电话给用户行政归属,直线admins = 用户。

<>strong>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
    @admins = User.where(:admin => "t")
  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 admins
    @admins=User.where(:admin => "t")
  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>。

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

<>strong>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+-.]+@[a-zd-.]+.[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

admins.html.erb

<ul class="admins">
  <%= render @admins %>
</ul>
最佳回答

您可能不得不在您为用户/管理人员提供的路程中形成一条手法。 问题是,铁路已经将一条路线(GET)/用户/*连接到用户专用冰箱的路面,因为你通常希望对用户做一次演示(例如,用户2/222)。

提 出

match  /users/admins ,  :to =>  users#admins 
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...