0

定数はどこで初期化しますか? コントローラーだけだと思ってた

エラー

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+\-.]+@[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

ユーザーページに移動すると、users/1 に移動すると上記のエラーが発生します。

4

1 に答える 1

7

ここにはいくつか問題があります-

  1. で定義されているように、AdminUserモデルを呼び出す必要があります。モデルを見つけようとしているため、エラーが発生します。コントローラはクラスを定義しません。Useruser.rbUsersControlleruninitialized constant UsersController::UserUser

  2. indexでアクションを定義していませんUsersControllerが、そのルートを定義しました。ファイルでリソースを宣言するとroutes.rb、Railsはデフォルトで7つのルートを作成します。これは、コントローラー内の特定のアクション(、、、、、、、、、およびindex)を指します。パラメータを使用して、Railsが1つ以上のルートを定義しないようにすることができます。たとえば、定義されたルートと、それらが呼び出すコントローラーアクションを確認できます。デフォルトでアクションをヒットしますが、デフォルトでアクションをヒットし、パラメータとして渡します。showneweditcreateupdatedelete:onlyresources :users, :only => [:new, :show]rake routeshttp://localhost:3000/usersUsersController#indexhttp://localhost:3000/users/1UsersController#show1id

于 2012-06-02T04:35:16.397 に答える