2

내 응용 프로그램에서 사람들이 작성할 수있는 게시물이 다릅니다. 그래서 통합 할 생각이에 대한 Single Table Inheritance했다 :레일 : 초기화되지 않은 상수 PostsController :: TextPost

class Post < ActiveRecord::Base 
    has_many :comments 
end 

class TextPostValidator < ActiveModel::Validator 
    def validate(record) 
    if record.title.nil? and record.body.nil? 
     record.errors[:base] << "Either title or body is necessary" 
    end 
    end 
end 

class TextPost < Post 
    validates_with TextPostValidator 
end 

class ImagePost < Post 
    validates :image_url, :presence => true 
end 

class VideoPost < Post 
    validates :video_code, :presence => true 
    validates :video_service, :presence => true 
end 

class LinkPost < Post 
    validates :link_url, :presence => true 
end 

을 그리고 내가 지금이 작업을 수행 할 때 내는 PostsController :

def new_text 
    @post = TextPost.new 
end 

def new_image 
    @post = ImagePost.new 
end 

def new_video 
    @post = VideoPost.new 
end 

def new_link 
    @post = LinkPost.new 
end 

나는이 오류가 :

uninitialized constant PostsController::TextPost 

이 보인다 이유를 찾기 위해 Rails의 내부 동작에 대해 충분히 알지 못합니다. rails console에서

추가 :

irb(main):009:0* ActiveRecord::Base.subclasses 
=> [Post(id: integer, title: string, body: text, video_service: string, video_code: string, image_url: string, link_url: string, ooc: boolean, nsfw: boolean, allow_comment: boolean, type: string, created_at: datetime, updated_at: datetime), 
TextPost(id: integer, title: string, body: text, video_service: string, video_code: string, image_url: string, link_url: string, ooc: boolean, nsfw: boolean, allow_comment: boolean, type: string, created_at: datetime, updated_at: datetime), 
ImagePost(id: integer, title: string, body: text, video_service: string, video_code: string, image_url: string, link_url: string, ooc: boolean, nsfw: boolean, allow_comment: boolean, type: string, created_at: datetime, updated_at: datetime), 
VideoPost(id: integer, title: string, body: text, video_service: string, video_code: string, image_url: string, link_url: string, ooc: boolean, nsfw: boolean, allow_comment: boolean, type: string, created_at: datetime, updated_at: datetime) 
LinkPost(id: integer, title: string, body: text, video_service: string, video_code: string, image_url: string, link_url: string, ooc: boolean, nsfw: boolean, allow_comment: boolean, type: string, created_at: datetime, updated_at: datetime)] 

괜찮아 보인다.

+0

@mu 아니요. – Lanbo

+0

@ 뮤 : 둘 다 작동하지 않습니다. 그것들은 또한'rails console'에 의해로드되지 않지만'post.rb'는 올바른 폴더에 있습니다. 'Post'가 올바르게 발견되고 사용할 수 있습니다. – Lanbo

+0

'Module.constants'는'Post'와'Post.constants'와 같은 하위 클래스가 없습니다. 질문에'ActiveRecord :: Base.subclasses'를 복사했습니다. – Lanbo

답변

5

라우팅 오류에서 초기화되지 않은 상수 오류가 발생합니다. routes.rb 파일로 이동하여 단일 및 복수 자원을 제공하십시오.

자원 : 포스트 및 자원 : 게시물

0

: http://www.hackido.com/2009/03/quick-tip-solve-uninitialized-constant.html

"루비 온 레일즈의 최신 릴리스와 함께 제공되는 주요 변화 중, 알고 보니는 몇 가지 사소한 사람이 있었다 그 중 하나는 응용 프로그램 컨트롤러가 더 이상 application.rb가 아니라는 것입니다. 이제 application_controller.rb라고합니다.

이 문제를 해결하려면 파일의 이름을 바꾸거나 Liam이 아래의 코멘트는 다음과 같이 실행합니다. "

레이크 레일 : 업데이트