내 응용 프로그램에서 사람들이 작성할 수있는 게시물이 다릅니다. 그래서 통합 할 생각이에 대한 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)]
괜찮아 보인다.
@mu 아니요. – Lanbo
@ 뮤 : 둘 다 작동하지 않습니다. 그것들은 또한'rails console'에 의해로드되지 않지만'post.rb'는 올바른 폴더에 있습니다. 'Post'가 올바르게 발견되고 사용할 수 있습니다. – Lanbo
'Module.constants'는'Post'와'Post.constants'와 같은 하위 클래스가 없습니다. 질문에'ActiveRecord :: Base.subclasses'를 복사했습니다. – Lanbo