【发布时间】:2014-08-22 01:27:00
【问题描述】:
我在我的 Rails 应用程序中有 BlogPosts 和 BlogCategories,还有一个 BlogPostCategorization 表将它们连接在一起。所以
class BlogCategory < ActiveRecord::Base
attr_accessible :name, :created_at, :updated_at, :blog_post_id
validates :name, presence: true, uniqueness: true
has_many :blog_post_categorizations
has_many :blog_posts, :through => :blog_post_categorizations
accepts_nested_attributes_for :blog_posts, allow_destroy: true
end
class BlogPost < ActiveRecord::Base
attr_accessible :body, :created_at, :updated_at, :image_url, :title
validates :body, :image_url, :title, presence: true
validates :title, uniqueness: true
has_many :blog_post_categorizations
has_many :blog_categories, :through => :blog_post_categorizations
accepts_nested_attributes_for :blog_categories, allow_destroy: true
end
class BlogPostCategorization < ActiveRecord::Base
belongs_to :blog_post
belongs_to :blog_category
end
现在通过 ActiveAdmin,我希望能够创建一个新的博客文章,并为此博客文章创建类别。我有
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Blog Post" do
f.input :title
f.input :body, as: :html_editor
f.input :image_url
end
f.inputs "Blog Categories" do
f.has_many :blog_post_categorizations do |s|
s.input :blog_category
end
end
f.actions
end
但是当我尝试访问新博客帖子的活动管理页面时,我收到一条 rails 错误消息,上面写着“未定义的方法 `new_record?' for nil:NilClass" 就在哪里
f.has_many :blog_post_categorizations do |s|
我做错了什么/错过了什么?
此外,下面还包括在 POST 请求的 params 哈希中发送的 blog_category 数据
"blog_categories_attributes"=>{"1408936652467"=>{"name"=>"cooking"}, "1408936656066"=>{"name"=>"eat"}}
【问题讨论】:
-
你使用什么版本的 Rails?
-
我使用的是 Rails 3.2.17
标签: ruby-on-rails ruby many-to-many schema activeadmin