【问题标题】:Error while trying to implement edit in rails 3.2尝试在 rails 3.2 中实现编辑时出错
【发布时间】:2013-03-12 11:39:39
【问题描述】:

我正在尝试在我的项目中编辑一个名为 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

谁能帮我解决这个问题

【问题讨论】:

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


【解决方案1】:

将字段(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

【讨论】:

  • 显示你的CategoriesController#update 方法实现?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
相关资源
最近更新 更多