这是我第一次使用单表继承。我正在尝试使用填充有帮助程序方法的选择菜单为博客文章设置子类。创建Post记录时,我总是遇到相同的错误。
There is an Error: Invalid single-table inheritance type: News is not a subclass of Post
这是我的模特
后
class Post < ActiveRecord::Base
scope :event, -> { where(type: 'Event') }
scope :news, -> { where(type: 'News') }
end
news_post.erb
class News < Post
end
event_post.erb
class Event < Post
end
post_helpers.erb
def post_types
[
['News'],
['Event'],
]
end
_form.erb
= simple_form_for @post do |f|
= f.select(:type, post_types { }, {}, { multiple: false , class: " default_select form-control " })
#rest of the form redacted
我缺少定义子类的东西吗?再次...第一次这样做...
更新:是的,表架构中有一个post.type列。它是一个字符串。
您必须重新定义post_type方法。
最好使用一个常数
TYPES = %w( News, Event )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句