无法大规模分配受保护的属性

问题描述 投票:2回答:3

我正在为我的一张表创建种子数据,并且每当我运行rake db:seed时,都会给我错误:

无法大规模分配受保护的属性:严重性

我的两个模型看起来像

class Status < ActiveRecord::Base
  belongs_to :severity
  attr_accessible :description, :image, :name, :slug, :severity_id
end

class Severity < ActiveRecord::Base
  attr_accessible :name, :val, :severity_id
end

我尝试使用的数据是

statuses = Status.create(
  [
    {
      "name"=> 'Normal', 
      "slug"=> 'normal', 
      "description"=> 'The service is up or was up during this entire period', 
      "severity"=> 1,
      "image"=> 'tick-circle'
    }
  ]
)

为什么会这样?

ruby-on-rails ruby mass-assignment
3个回答
5
投票

您需要在attr_accesible行的Severity模型中添加:severity。 Rails尝试使用该名称(我认为您在数据库中具有的名称)分配属性。


2
投票
attr_accessible :severity

第6节:批量分配http://guides.rubyonrails.org/security.html


1
投票

您的种子说severity,但访问者说severity_id。那是哪一个?

© www.soinside.com 2019 - 2024. All rights reserved.