Rails 5有很多通过嵌套表单和Cocoon

时间:2017-03-29 23:23:40

标签: ruby-on-rails nested-forms cocoon-gem

求解:见底

我正在构建许多产品的订购系统,其中HBTM从Join Table到anotherModel,但在创建和编辑嵌套表单的新记录时我遇到了一些问题。

我尝试了其他问题的许多解决方案,并遵循了thisthis等教程,但没有解决我的问题。

产品型号:

class Product < ApplicationRecord
  belongs_to :subcategory

  has_many :order_products, inverse_of: :product
  has_many :order, :through => :order_products
end

订单型号:

class Order < ApplicationRecord
  has_many :order_products, inverse_of: :order
  has_many :products, :through => :order_products

  accepts_nested_attributes_for :products, reject_if: :all_blank, allow_destroy: true
end

加入表:

class OrderProduct < ApplicationRecord
  belongs_to :order
  belongs_to :product

  has_and_belongs_to_many :ingredients
  accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true    
end

与联接表相关联的成分模型:( 多对多以保存订单中每个产品的添加成分)

class Ingredient < ApplicationRecord
  has_and_belongs_to_many :order_products
end

订单控制器:

  # GET /orders/new
  def new
    @order = Order.new
  end

  # GET /orders/1/edit
  def edit
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

    def order_params
      params.require(:order).permit(:subtotal, :discount, :total,
      order_products_attributes: [[:id, :order_id, :product_id , :_destroy]])
    end

查看:

<%= simple_form_for(@order) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :subtotal %>
    <%= f.input :discount %>
    <%= f.input :total %>
  </div>

  <%= f.simple_fields_for :order_products do |o| %>
      <%= render 'order_product_fields', f: o %>
  <% end %>

  <%= link_to_add_association "aggiungi prodotto", f, :order_products %>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

部分:(由于自动添加空白字段,无法添加cocoon的销毁链接:未定义方法`reflect_on_association'为NilClass:Class

<div class="nested-fields order-product-fields">
  <%= f.input :product %>
  <%= f.input :order %>

  <%= f.check_box :_destroy %>

</div>

我在控制台上得到了这个:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"PRNzrnxk/jrsAyPK5OzFxix2hIUmjCeWpD8Fkuhuhx8Wp3/xYTbTTvsfQkhnMDEBNnC/iNL46kl68XR7skb03g==", "order"=>{"subtotal"=>"", "discount"=>"", "total"=>"", "order_products"=>{"product"=>"", "order"=>"", "_destroy"=>"0"}}, "commit"=>"Create Order"}
Unpermitted parameter: order_products

我的烦恼是:编辑:每个点上的解决方案

  1. 当我加载订单页面时,会自动创建嵌套属性的空白字段()
  2. 订单模型上的nested_attributes错误:

    accepts_nested_attributes_for **:order_products**, reject_if: :all_blank, allow_destroy: true
    
    1. 提交表单时,不会创建任何条目。 (但我可以通过控制台添加产品.ex:order.products&lt;&lt; product1)

    2. 当我通过cocoon的添加链接添加更多产品时,仅首先发送到控制器

    3. 不知道如何在Order
    4. 形式内实现Ingredient属性的强参数和视图

      有这个工作:

      成分模型:

      has_many :ingredient_order_products, inverse_of: :ingredient
      has_many :order_products, :through => :ingredient_order_products
      

      IngredientOrderProduct型号:

        belongs_to :ingredient
        belongs_to :order_product
      

      订购产品型号:

        has_many :ingredient_order_products, inverse_of: :order_product
        has_many :ingredients, :through => :ingredient_order_products
        accepts_nested_attributes_for :ingredient_order_products, reject_if: :all_blank, allow_destroy: true
      

      订单控制器:

      def order_params
        params.require(:order).permit(:customer_id, :subtotal, :discount, :total,
        order_products_attributes: [[:id, :order_id, :product_id, :qty, :gift, :_destroy,
          ingredient_order_products_attributes: [:id, :order_id, :ingredient_id, :_destroy]]])
      end
      

      _order_product_fields部分:

        <div id="ingredient">
          <%= f.simple_fields_for :ingredient_order_products do |ingredient| %>
            <%= render 'ingredient_order_product_fields', f: ingredient %>
          <% end %>
          <%= link_to_add_association "Aggiungi Ingrediente", f, :ingredient_order_products %>
        </div>
      

0 个答案:

没有答案