2016-09-21 3 views
0

user_types 모델 (id 및 string을 type으로 표시)에 의해 설명되는 권한이 다른 사용자 모델이있는 앱을 설치합니다.Rails가 상수를 자동로드 할 수 없습니다. user_type

: 아래
class UserType < ActiveRecord::Base 
    belongs_to :user 
    validates :type, presence: true, length: { maximum: 100 } 
end 

컨트롤러 및 테스트 파일을

class UserTypeController < ApplicationController 
    def index 
     @user_type = User_type.all 
    end 
    def new 
     @user_type = User_type.build(user_type_params) 
    end 
    private 
    def user_type_params 
     params.require(:user_type).permit(:type) 
    end 
end 

테스트 모델 : 나는 레일 테스트를 실행하고, 나는 오류

여기
LoadError: Unable to autoload constant User_type, expected app/models/user_type.rb to define it 

을 얻고 나의 모델입니다

require 'test_helper' 

class UserTypeTest < ActiveSupport::TestCase 
    # test "the truth" do 
    # assert true 
    # end 
    def setup 
    @user_type = User_type.new(id:2,type:"test") 
    end 
    test "is_valid" do 
    assert @user_type.valid? 
    end 

end 

나는 기본적인 "is_va 뚜껑 "테스트 및 위에서 설명한 오류가 발생했습니다. 나는 또한 마지막 모델 "user_type"을 삭제하고 "UserType"으로 만들었지 만 작동하지 않았습니다.

+0

UserType 모델은 app/models/user_type.rb에 있습니까? –

답변

0

모델의 클래스 이름은 UserType입니다. User_type은 어디에도 정의되어 있지 않습니다. 모델 이름 그대로 유지하고 테스트 및 컨트롤러를 수정하거나 모델 이름을 바꿉니다. 전자는 Rails 대회이므로, 나는 그걸 가지고 가자고 제안합니다 (CamelCase 이름).

+0

Allright, but : 1) User_type 모델 이름이었는데 이전에 변경했습니다. 2) @ UserType = UserType.new를 사용한 후에이 오류가 발생합니다. ActiveRecord :: SubclassNotFound : 잘못된 단일 테이블 상속 유형 : test가 UserType 서브 클래스가 아님 – andrey

+0

단일 테이블 상속을 구현하려는 경우'type'이 기본 속성 이름입니다. 열과 속성 접근 자의 이름을 다른 것으로 변경하거나 (예 :'kind') 또는 상속 열을 변경할 수 있습니다. 참조 : http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-inheritance_column – Teoulas

0

컨트롤러 및 테스트 전체에서 User_type.all, User_type.build, User_type.new을 사용하지만 모델 이름은 UserType입니다.

0

변경 type 열을 제거하거나 그 예약 된 열 이름 때문에 user_type 같은 이름을 바꿉니다 클래스에서 그 UserType 모델의 테이블에서 정의 된 UserType에 모델 이름, 단일 테이블 상속으로 만 사용할 수 있습니다.

+0

내가 가지고있는 것 : NoMethodError : # 에 대한 정의되지 않은 메소드 'type' 테스트/모델 /user_type_test.rb:11:in'Block in '내 user_type 테스트 : def setup \t @UserType = UserType.new (id : 2, description : "테스트") end 테스트 "check"do \t assert @ UserType.valid? end – andrey

+0

마이그레이션을 사용하여'type' 열의 이름을 바꿨습니까? 'UserType' 컨트롤러 (user_type_params에서)와 모델 (존재 확인에서)을 포함하여 모든 곳에서'type'의 사용을 제거 했습니까? – Sajan

관련 문제