ruby-on-rails-4 - ActiveAdmin has_many 通过关系不更新参数 Rails

标签 ruby-on-rails-4 activeadmin has-many-through formtastic

我正在通过事件管理中的关系创建一个 has_many: 。以下是目前的模型:

 class Category < ActiveRecord::Base
   has_many :subcategories
 end

 class Subcategory < ActiveRecord::Base
   has_many :product_in_subcategories
   has_many :products, through: :product_in_subcategories
   accepts_nested_attributes_for :product_in_subcategories, :allow_destroy => true

   belongs_to :category
 end

 class Product < ActiveRecord::Base
   has_many :product_in_subcategories
   has_many :subcategories, through: :product_in_subcategories
   accepts_nested_attributes_for :product_in_subcategories, :allow_destroy => true
 end

 class ProductInSubcategory < ActiveRecord::Base
   belongs_to :product
   belongs_to :subcategory
 end

在ActiveAdmin中,我有permit_params和表单,如下所示:

 ActiveAdmin.register Product do
   # note some params that are product only have been removed for simplicity
   permit_params :name, subcategory_id:[:id], product_in_subcategories_attributes: [:id, :subcategory_id, :product_id, :_create, :_update]

     form do |f|
       f.inputs

      f.has_many :product_in_subcategories do |s|
          s.input :subcategory_id, :as => :check_boxes, :collection => Subcategory.all
      end

      f.actions
    end
end

表单按预期填充,并将保存除 subcategory_id 之外的所有内容。如果我在数据库中输入正确的 subcategory_id,该框将在编辑时显示选中状态。

保存时的消息给出:

 Unpermitted parameters: subcategory_id

但是,它似乎正在尝试将其与产品一起提交,但该产品没有 subcategory_id。关于我在这里做错了什么有什么想法吗?这让我发疯,我已经阅读了我能找到的所有内容。我真的很想了解我做错了什么。谢谢。

最佳答案

在这个问题上花了很多时间之后,除了这个之外我找不到合适的解决方案,这实际上非常好。事实上,它与我设想的解决方案没有太大区别:

对上述代码的唯一更改是在 ActiveAdmin 中进行的:

 ActiveAdmin.register Product do
    # note some params that are product only have been removed for simplicity
    permit_params :name, product_in_subcategories_attributes: [:id, :subcategory_id, :product_id, :_create, :_update]

      form do |f|
        f.inputs

        f.has_many :product_in_subcategories do |s|
           s.input :subcategory_id, :as => :select, :collection => Subcategory.all
        end

        f.actions
      end
 end

非常奇怪的是,这允许选择框没有任何问题,但它会翻转复选框。尽管如此,我对这个解决方案很满意。

关于ruby-on-rails-4 - ActiveAdmin has_many 通过关系不更新参数 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29810223/

相关文章:

sql - 没有时间戳的检索

javascript - ActionView::Template::Error ("javascript file"未预编译)

ruby-on-rails - 事件管理嵌套表单创建重复记录

ruby-on-rails - 如何在 Rails 3 中为社交网络应用程序实现友谊模型?

ruby-on-rails - rails : has_many through with polymorphic association - will this work?

ruby-on-rails - 为 has_many :through relationship in Rails 3 创建 Controller 和 View

javascript - 如何使用按钮将时间传递给 Postgresql DB 而不是表单中的 rails time_select 框

ruby-on-rails - Rails 4 枚举验证

ruby-on-rails - rails : will_paginate Undefined method `paginate'

用于自定义索引记录的 Activeadmin 自定义过滤器和范围 Rails 4