Rails:未初始化的常量 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 控制台
:
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)]
看起来没问题。
In my App, I have different kinds of posts people can make. So I had the idea to incorporate the Single Table Inheritance for this:
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
And when I now do this in my 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
I get this error:
uninitialized constant PostsController::TextPost
It seems I know not enough about the inner workings of Rails to find out why.
Addition
From the 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)]
Seems ok.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
未初始化的常量错误属于路由错误。尝试转到routes.rb 文件并提供单数和复数资源。
资源:帖子
和
资源:帖子
An uninitialized constant error comes under a Routing Error. Try going to your routes.rb file and offering both singular and pluralized resources.
resources :post
and
resources :posts
来自: http://www.hackido.com/2009 /03/quick-tip-solve-uninitialized-constant.html
“事实证明,在最新版本的 Ruby on Rails 带来的主要变化中,有一些其中之一是应用程序控制器不再称为 application.rb 现在它被称为 application_controller.rb
实际上,要解决此问题,只需重命名该文件即可。在下面,运行:”
rakerails:update
From : http://www.hackido.com/2009/03/quick-tip-solve-uninitialized-constant.html
"Turns out, among the Major changes that came along with the latest Release of Ruby on Rails, there were a few minor ones too. One of those is that the application controller is no longer called application.rb Now it's referred to as application_controller.rb.
In effect, to solve this problem, just rename the file. Or, as Liam points out in the comments below, run:"
rake rails:update