2012-06-02 64 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參數。

相關問題