尝试在rails 3.2中实现编辑时出错

时间:2013-03-12 11:39:40

标签: ruby-on-rails ruby-on-rails-3 html5 ruby-on-rails-3.2

我正在尝试在我的项目中编辑一个名为categories的视图,但在尝试这样做时我似乎遇到了一个非常奇怪的错误。

显示的错误如下: -

    ActiveModel::MassAssignmentSecurity::Error in CategoriesController#update

    Can't mass-assign protected attributes: id, name, parent_cat_id

Parameters:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"RaQQRvJRRmjg0HAy3utlrD2wKAvYXbtNC2hjR4JHpXs=",
 "category"=>{"id"=>"3",
 "name"=>"Non-Fictional",
 "parent_cat_id"=>"0"},
 "commit"=>"Update",
 "id"=>"3"}

我的类别表如下: -

id serial NOT NULL,
  name character varying(255),
  parent_cat_id integer DEFAULT 0,
  deleted integer NOT NULL DEFAULT 0,
  CONSTRAINT categories_pkey PRIMARY KEY (id)

我的类别模型文件如下: -

class Category < ActiveRecord::Base

    attr_accessible :name, :parent_cat_id

    # ------- ASSOCIATIONS -----------

    has_many :books

    belongs_to :category, :foreign_key => 'parent_cat_id'
end

任何人都可以帮助我这个

1 个答案:

答案 0 :(得分:0)

将字段(id,name,parent_cat_id)添加到Category model,这应该是批量分配的 -

class Category < ActiveRecord::Base
  attr_accessible :name, :parent_cat_id, :id
    # ------- ASSOCIATIONS -----------
  has_many :books
  belongs_to :category, :foreign_key => 'parent_cat_id'
end