2016-06-29 133 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