【问题标题】:Nested association has_many;through doesn't update collection_check_boxes嵌套关联 has_many;through 不更新 collection_check_boxes
【发布时间】:2019-04-06 18:41:33
【问题描述】:

使用复选框更新嵌套表单我无法更新表格。我收到以下消息:

不允许的参数::category
动作控制器::参数 {"name"=>"Flux Capacitor", "price"=>"19.55"} 允许:true

我尝试了不同的方法通过允许的参数来解决这个问题,包括:category 参数,如下所示:

def product_params
  params.require(:product).permit(:id, :name, :price, :category, categories_attributes: [:id, :name, :category], categorizations_attributes: [:id, :product_id, :category_ids, :category])
end

我的模型

class Product < ApplicationRecord
  has_many :categorizations
  has_many :categories, through: :categorizations
  accepts_nested_attributes_for :categories, reject_if: proc {|attributes| attributes['name'].blank?}
  accepts_nested_attributes_for :categorizations
end
class Categorization < ApplicationRecord
  belongs_to :product, inverse_of: :categorizations
  belongs_to :category, inverse_of: :categorizations
end
class Category < ApplicationRecord
  has_many :categorizations
  has_many :products, through: :categorizations, inverse_of: :category
end
class ProductsController < ApplicationController

  def edit
    @categories = Category.all
  end

  def new
   @product = Product.new
  end

  def create
    @product = Product.new(product_params)
    if @product.save
      flash[:notice] = 'Product succesfully created'
      redirect_to products_path
    else
      flash[:notice] = 'Product was not created'
      render 'edit'
    end
  end

  def update
    if @product.update(product_params)
      flash[:notice] = "Product succesfully updated"
      redirect_to products_path
    else
      flash[:notice] = 'Product was not updated'
      render 'edit'
    end
  end

app/view/products/edit.html.erb

<%= simple_form_for(@product) do |f| %>
  <%= f.input :name %>
  <%= f.input :price %>
  <%= f.simple_fields_for @product.categories do |cats| %>
    <%= cats.collection_check_boxes  :ids, Category.all, :id, :name, collection_wrapper_tag: :ul, item_wrapper_tag: :li %>
  <% end %>
  <%= f.button :submit %>
<% end %>

这似乎很常见,rails 和/或 simple_form 应该以更内置的方式提供来执行此操作。我错过了什么明显的东西吗?

【问题讨论】:

    标签: ruby-on-rails ruby nested simple-form


    【解决方案1】:

    如果我对您的理解正确,您应该能够在不使用 Accepts_nested_attributes_for 或 simple_fields_for 的情况下执行此操作。试试这样的:

    <%= simple_form_for(@product) do |f| %>
      <%= f.input :name %>
      <%= f.input :price %>
      <%= f.association :categories, as: :check_boxes %>
      <%= f.button :submit %>
    <% end %>
    

    您的强参数应如下所示:

    def product_params
      params.require(:product).permit(:id, :name, :price, { category_ids: [] }])
    end
    

    【讨论】:

    • 它有效!太好了,非常感谢@Ben_Trewem。但现在我更困惑了。 1)为什么不需要'accepts_nested_attributes_for'助手,显然是两个模型的嵌套形式? .大概是由于simple_form的关联字段类型。 2)为什么不使用类别参数,而是必须使用'category_ids'将参数传递给产品的更新方法。 3) 允许简单的凡人学习和理解这些问题的机制和概念的信息链接在哪里?再次感谢。
    • 您得到的是产品和类别之间的habtm 关联,使用分类作为连接表。 simple_form 支持这些类型的关联,只需将类别 ID 传递给关联。您必须将 category_ids 添加到强参数允许中,以允许在您的创建/更新操作中批量分配这些参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多