Ruby on rails 未初始化的常量UsersController::User
在哪里初始化常量?我以为它就在控制器里 错误 用户控制器Ruby on rails 未初始化的常量UsersController::User,ruby-on-rails,Ruby On Rails,在哪里初始化常量?我以为它就在控制器里 错误 用户控制器 class UsersController < ApplicationController def show @user = User.find(params[:id]) end def new end end user.rb class AdminUser < ActiveRecord::Base attr_accessible :na
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
end
end
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+\-.]+@[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
当我转到用户页面时,但当我转到用户/1时,我会出现上述错误。这里有几个问题-
AdminUser
模型应该被称为User
,这是在User.rb
中定义的,并且您的userscoontroller
正在尝试查找它们,这就是为什么您会得到未初始化常量userscoontroller::User
错误。控制器不会为您定义用户
类UsersController
中定义索引
操作,但已为其定义了路由。当您在routes.rb
文件中声明资源时,Rails将默认创建7条路由,这些路由指向控制器中的特定操作-索引
,显示
,新建
,编辑
,创建
,更新
,以及删除
。您可以通过:only
参数阻止Rails定义一个或多个路由-例如资源:用户,:only=>[:new,:show]
您可以看到已定义的路由,以及它们将使用rake路由调用的控制器操作<代码>http://localhost:3000/users默认情况下,
将点击用户控制器#索引
操作,而http://localhost:3000/users/1默认情况下,
将点击userscoontroller#show
操作,将1
作为id
参数传递你这里有几个问题-
AdminUser
模型应该被称为User
,这是在User.rb
中定义的,并且您的userscoontroller
正在尝试查找它们,这就是为什么您会得到未初始化常量userscoontroller::User
错误。控制器不会为您定义用户
类UsersController
中定义索引
操作,但已为其定义了路由。当您在routes.rb
文件中声明资源时,Rails将默认创建7条路由,这些路由指向控制器中的特定操作-索引
,显示
,新建
,编辑
,创建
,更新
,以及删除
。您可以通过:only
参数阻止Rails定义一个或多个路由-例如资源:用户,:only=>[:new,:show]
您可以看到已定义的路由,以及它们将使用rake路由调用的控制器操作<代码>http://localhost:3000/users默认情况下,
将点击用户控制器#索引
操作,而http://localhost:3000/users/1默认情况下,
将点击userscoontroller#show
操作,将1
作为id
参数传递你会在app/models/user.rb中发布代码吗?堆栈跟踪会很有用…你会在app/models/user.rb中发布代码吗?堆栈跟踪会很有用。。。
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'
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+\-.]+@[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