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

Form-view 所以我最终通过删除产品 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>

我在这里做错了什么?

4

1 回答 1

0

我认为这就像添加:products:category_departments喜欢一样简单attr_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 没有类别对象作为参数。

于 2013-10-20T13:23:37.093 回答