Rails:未初始化的常量 PostsController::TextPost

发布于 2024-11-30 17:40:48 字数 2523 浏览 1 评论 0原文

在我的应用程序中,人们可以发布不同类型的帖子。所以我想到了合并 单表继承为此:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-12-07 17:40:48

未初始化的常量错误属于路由错误。尝试转到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

我的奇迹 2024-12-07 17:40:48

来自: 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文