2013-10-20 40 views
0

我一直停留在得到嵌套屬性了一會兒Rails的3.2.14和已經看了很多例子工作,我似乎仍不能得到它上班。在當我嘗試提交我得到以下錯誤的形式時刻:嵌套屬性:無法大規模指派保護屬性

ActiveModel::MassAssignmentSecurity::Error in Admin::CategoriesController#create 

Can't mass-assign protected attributes: products, category_departments 

這裏是我的代碼:

class Category < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :category_name, use: :slugged 
    attr_accessible :category_name, :products_attributes, :slug, :department_id,  :category_departments_attributes 

    has_many :products 
    has_many :category_departments 
    has_many :departments, :through => :category_departments 

    validates_presence_of :category_name 

    accepts_nested_attributes_for :products 
    accepts_nested_attributes_for :category_departments 

    scope :department_category, lambda {|department| joins(:department, :products).where("departments.department_name" => department) } 
end 

控制器:

class Admin::CategoriesController < ApplicationController 

     def new 
     @category = Category.new 
    @category.products.build 
    @category.category_departments.build 
    end 

def create 
    @category = Category.new(params[:category]) 
    if @category.save 
    redirect_to admin_products_path 
    else 
    flash.now[:error] = "Could not save the category" 
    render "new" 
    end 
end 

表格視圖 所以最後通過刪除產品fields_for來解決此問題,並允許從產品創建頁面中選擇部門。

Form:- 
    <h1> Create a Category </h1> 
    <%= form_for :category, :url => { :action => "create" }, :method => :post do |f| %> 
    <%= render :partial => 'form_category', :locals => {:f => f} %> 
    <% end %> 

Partial:- 
    <div class="category-form-update"> 
<form class="form-horizontal"> 

    <div class="control-group"> 
     <label class="control-label"> 
      <%= f.label :category_name %> 
     </label> 
     <div clas="controls"> 
      <%= f.text_field :category_name %> 
     </div> 
    </div> 
<div class="control-group"> 
     <%= f.fields_for :category_departments do |builder| %> 
     <label class="control-label"> 
      <%= builder.label :department, "Department" %> 
     </label> 
     <div class="controls"> 
      <% department = Department.all.map { |dep| [dep.department_name.capitalize, dep.id] } %> 
     <%= builder.select(:department_id ,options_for_select(department)) %> 
     <% end %> 
     </div> 
    </div> 

    <div class="control-group"> 
     <div class="controls"> 
      <button class="btn"> 
       <%= f.submit %> 
      </button> 
     </div> 
    </div> 
</div> 

我在這裏做得不對?

+0

你在視圖中使用何種方式構建表單?我希望看到類似'<%= form_for',但我看到

'。 –

+0

多數民衆贊成在部分,但我使用form_for建立表格 –

+0

因此,你有一個窗體內的形式?對於嵌套屬性,生成的輸出是什麼樣的?字段名稱是否以「_attributes」結尾? –

回答

0

我想這是因爲添加:products:category_departmentsattr_accessible像這樣一樣簡單:

class Category < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :category_name, use: :slugged 
    attr_accessible :category_name, :products_attributes, :slug, :department_id, :category_departments_attributes, :products, :category_departments 

... 

而不檢查內部,我認爲是因爲nested_attributes魔法使用:products_attributes填充:products這是必要的。

編輯:

這隻揭示了實際問題,它是嵌套屬性命名魔術並沒有踢,正確命名的字段。我的猜測是form_for沒有將類別對象作爲參數。

+0

[文檔](http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for)似乎說'products_attributes'應該足夠了。 – tihom

+0

是的。這也許不完全是正確的,不過,因爲也許[這種方法](http://apidock.com/rails/ActiveRecord/NestedAttributes/assign_nested_attributes_for_collection_association)仍然使用實際的(保護)協會。不確定。 – CMW

+0

我增值產品和category_departments到attr_accessible和得到這個錯誤: - 的ActiveRecord :: AssociationTypeMismatch在管理:: CategoriesController#創建 產品(#70329550591740)預期,得到了陣列(#70329502699820) –

相關問題