Ada*_*fin 3 ruby schema many-to-many ruby-on-rails activeadmin
我在我的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
Run Code Online (Sandbox Code Playgroud)
现在通过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
Run Code Online (Sandbox Code Playgroud)
但是当我尝试访问新的Blog Post的活动管理页面时,我收到一条轨道错误消息"未定义的方法`new_record?' 为零:NilClass"就行了
f.has_many:blog_post_categorizations do | s |
我做错了什么/错过了什么?
此外,下面包含的是在POST请求的params哈希中发送的blog_category数据
"blog_categories_attributes"=>{"1408936652467"=>{"name"=>"cooking"}, "1408936656066"=>{"name"=>"eat"}}
Run Code Online (Sandbox Code Playgroud)
我认为这部分:
f.inputs "Blog Categories" do
f.has_many :blog_post_categorizations do |s|
s.input :blog_category
end
end
Run Code Online (Sandbox Code Playgroud)
应该:
f.inputs "Blog Categories" do
f.has_many :blog_categorys do |s|
s.input :name
end
end
Run Code Online (Sandbox Code Playgroud)
考虑到你在这个问题上的另一个问题,我会建议一个(我希望)好的解决方法.由于strong_parameters默认在Rails 4及更高版本中使用,并且您使用的是Rails 3.2.17,因此我们让您的应用程序使用它.所以有步骤:
安装gem 'strong_parameters';
设置为false config.active_record.whitelist_attributes的config/application.rb;
include ActiveModel::ForbiddenAttributesProtection在BlogPost和BlogCategory模型;attr_accessible电话.完成所有这些后,您可以使用私有方法将所有参数列入白名单,如下所示:
private
def blog_post_params
params.require(:blog_post).permit(#all the params here)
end
Run Code Online (Sandbox Code Playgroud)
并且在#create action中使用BlogPost.new(blog_post_params)(在其他模型中使用包含的模块).现在您可以从strong_parameters中受益.
在ActiveAdmin BlogPost模型中,将所有允许的参数列入白名单:
permit_params :body, :created_at, :updated_at, :image_url, :title, blog_categories_attributes: [:id, :name, :_destroy, :blog_post_id]
Run Code Online (Sandbox Code Playgroud)
并在控制器中
controller do
def permitted_params
params.permit blog_posts: [:body, :created_at, :updated_at, :image_url, :title, blog_categories_attributes: [:id, :name, :_destroy, :blog_post_id]]
end
end
Run Code Online (Sandbox Code Playgroud)
更准确地了解文档中的strong_params用法.并且还要仔细检查错别字/错误的命名/下划线(因为我推测了一些事情).
祝好运!
| 归档时间: |
|
| 查看次数: |
3524 次 |
| 最近记录: |