2012-06-02 68 views
0

我在哪里初始化常量?我认为这只是在控制器中。未初始化的常量UsersController ::用户

错误

uninitialized constant UsersController::User 

用户控制器

class UsersController < ApplicationController 
     def show 
     @user = User.find(params[:id]) 
     end 
     def new 
     end 
    end 

路由

SampleApp::Application.routes.draw do 

    get "users/new" 
resources :users 
    root to: 'static_pages#home' 

    match '/signup', to: 'users#new' 

    match '/help', to: 'static_pages#help' 
    match '/about', to: 'static_pages#about' 
    match '/contact', to: 'static_pages#contact' 

user.rb

class AdminUser < ActiveRecord::Base 
     attr_accessible :name, :email, :password, :password_confirmation 
     has_secure_password 
     before_save { |user| user.email = email.downcase } 
     validates :name, presence: true, length: { maximum: 50 } 
     VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
     validates :email, presence: true, 
     format: { with: VALID_EMAIL_REGEX }, 
     uniqueness: { case_sensitive: false } 
     validates :password, presence: true, length: { minimum: 6 } 
     validates :password_confirmation, presence: true 
    end 

这可能有助于 我也越来越

The action 'index' could not be found for UsersController 

当我去到用户页面上,但是当我去到用户/ 1,我得到上述错误。

+1

你会在app/models/user.rb中发布代码吗? –

+0

堆栈跟踪会很有用... – eggie5

回答

6

你有几个问题在这里 -

  1. AdminUser模型应该被称为User,因为它在user.rb的已定义,和你UsersController试图找到他们,这就是为什么你得到的uninitialized constant UsersController::User错误。控制器不会为您定义User类。

  2. 您尚未在UsersController中定义index动作,但您已为其定义路线。当您在routes.rb文件中声明的资源,Rails会默认创建7个路由,指向具体行动控制器 - indexshowneweditcreateupdatedelete。您可以通过参数:only阻止Rails定义一个或多个路由 - 例如resources :users, :only => [:new, :show]您可以看到已定义的路线以及他们将使用rake routes调用的控制器操作。 http://localhost:3000/users会默认点击UsersController#index动作,而http://localhost:3000/users/1默认点击UsersController#show动作,通过1作为id参数。

相关问题