2016-06-29 89 views
0

我有一个品牌的模型,可以有许多产品可以有多个类别。我有一个嵌套表单来创建允许嵌套属性创建类别的产品。但我可以让它工作。初始化嵌套表格不能正常工作的对象

class Brand < ActiveRecord::Base 
    has_and_belongs_to_many :users 
    has_many :products, dependent: :destroy 

    validates :name, presence: true, 
        length: { maximum: 50 } 
end 

class Product < ActiveRecord::Base 
    belongs_to :brand 
    has_many :categories, dependent: :destroy 
    accepts_nested_attributes_for :categories 

    default_scope -> { order(created_at: :desc) } 

    validates :brand_id, presence: true 
    validates :name,  presence: true, 
         length: { maximum: 50 } 
    private 

    def product_params 
     params.require(:product).permit(:name, 
             categories_attributes: [:name, :price]) 
    end 
end 

class Category < ActiveRecord::Base 
    belongs_to :product 
    has_many :units, dependent: :destroy 

    validates :price, presence: true 
    validates :product_id, presence: true 
    validates :name,  presence: true, 
         length: { maximum: 50 } 
end 

所以我的产品控制器:

class ProductsController < ApplicationController 
    def new 
    @product = current_brand.products.new 
    @product.categories.build 
    end 

    def create 
    @product = current_brand.products.build(product_params) 
    if @product.save 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

和我的新看法是这样的:

<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <%= form_for(@product) do |f| %> 
     <%= render 'shared/error_messages_products' %> 

     <%= f.label :name, "Name:" %> 
     <%= f.text_field :name, class: 'form-control' %> 
     <%= link_to_add_fields "Add Category", f, :categories %> 

     <%= f.submit "Add Product", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 

和我的类别部分是:

<fieldset> 
    <%= f.label :name, "Category Name" %> 
    <%= f.text_field :name, class: 'form-control' %> 

    <%= f.label :price, "Price" %> 
    <%= f.text_field :price, class: 'form-control' %> 
    <hr> 
</fieldset> 

我已经link_to_add_fields助手在我的应用程序中通货膨胀帮手:

module ApplicationHelper 
    def link_to_add_fields(name, f, association) 
    new_object = f.object.send(association).klass.new 
    id = new_object.object_id 
    fields = f.fields_for(association, new_object, child_index: id) do |builder| 
     render(association.to_s.singularize + "_fields", f: builder) 
    end 
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) 
    end 
end 

,让我用一些JavaScript与添加类别字段:

jQuery -> 
    $('form').on 'click', '.add_fields', (event) -> 
    time = new Date().getTime() 
    regexp = new RegExp($(this).data('id'), 'g') 
    $(this).before($(this).data('fields').replace(regexp, time)) 
    event.preventDefault() 

但是,当我试图在这个例子2的产品添加任何数量的类别,我失败了创建产品和类别。我从我的形式和对象错误出现错误:

The form contains 1 error: 
Categories product can't be blank 

我从这个submition得到的PARAMS是:

{"utf8"=>"✓", "authenticity_token"=>"IO8GFcv1auFVh/ZNypONI78XQrY2Ntm07cMrrjmq51ogwppbsb1sNyN/ynKY+Pdb/lyniED9O6jFRkLKsvu2jQ==", "product"=>{"name"=>"Product Example", "categories_attributes"=>{"1467231299616"=>{"name"=>"Category Example 1", "price"=>"1234"}, "1467231300745"=>{"name"=>"Category Example 2", "price"=>"1234"}}}, "commit"=>"Agregar Producto", "controller"=>"products", "action"=>"create"} 

我不明白为什么类别和产品没有正确关联。

+0

如果你的关联存在问题,那么问题就会出现在你的模型中。我浏览了你的模型,并没有真正的出现在我身上,但是对ActiveRecord关联进行了一些研究。 – Chris

+0

好的谢谢你的提示让我检查出来! –

+0

我添加了我用于产品的强力参数,可能存在问题 –

回答

1

经过一段时间的实验后,我发现答案是从类别模型中删除product_id的验证。像这样:

class Category < ActiveRecord::Base 
    belongs_to :product 
    has_many :units, dependent: :destroy 

    validates :price, presence: true 
    validates :name,  presence: true, 
         length: { maximum: 50 } 
end