Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未初始化的常量UsersController::User_Ruby On Rails - Fatal编程技术网

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