【问题标题】:Rails ActiveAdmin form for many-to-many relationship through a join table not workingRails ActiveAdmin 表单通过连接表进行多对多关系不起作用
【发布时间】: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


【解决方案1】:

我认为这部分:

f.inputs "Blog Categories" do
  f.has_many :blog_post_categorizations do |s|
    s.input :blog_category
  end
end

应该是:

f.inputs "Blog Categories" do
  f.has_many :blog_categorys do |s|
    s.input :name
  end
end

编辑

考虑到您对此问题的另一个问题,我会推荐一个(我希望)好的解决方法。由于 strong_parameters 默认在 Rails 4 及更高版本中使用,并且您使用的是 Rails 3.2.17,让我们让您的应用程序使用它。 所以有以下步骤:

  • 安装gem 'strong_parameters'

  • config/application.rb 中设置为false config.active_record.whitelist_attributes

  • include ActiveModel::ForbiddenAttributesProtectionBlogPostBlogCategory 模型中;
  • 摆脱所有attr_accessible 呼叫。

完成所有这些后,您可以将私有方法中的所有参数列入白名单,如下所示:

private

def blog_post_params
  params.require(:blog_post).permit(#all the params here)
end

并且在#create 操作中使用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]

在控制器中

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

更准确地查看文档中 strong_params 的用法。还要仔细检查拼写错误/错误的命名/下划线(因为我假设了一些事情)。

祝你好运!

【讨论】:

  • 该更改使得新的 BlogPost 页面将加载,但是当我尝试提交添加了几个类别的新帖子时,我收到了 blogcategories 的 cannot mass-assign attributes 错误
  • 这意味着我们已经解决了最初的问题(太棒了!)但是有一个新问题。那就是您必须将嵌套模型的 id 属性列入白名单。因此将 :id 添加到 attr_accessible (这是它在 rails 4 中的工作方式,希望在 3.2.17 中也是如此......)到 blogcategory 模型
  • 我已经确保在 attr_accessible 下包含每个字段
  • 嘿,最近怎么样?有一段时间没上网了。如果您仍然遇到同样的错误,请尝试将允许的 params 方法添加到 activeadmin(也尝试包含所有允许的内容,包括:id)。像这个控制器一样 #... def allowed_pa​​rams params.permit(:blogposts => [:name, :description]) end end
  • 我使用的是 Rails 3.2,我相信 allowed_pa​​rams 适用于 Rails 4
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 2011-05-18
  • 2021-06-22
相关资源
最近更新 更多