我正在尝试创建一个 teacher
表,其中有一列名为“teacher_type_id”,即它是一个连接到 teacherType
表的外键,该表具有三行,即 1 => 导师,2 => 模块负责人和 3 => 讲师。
我的 schema.rb
文件中包含以下信息:
create_table "teacher_types", :force => true do |t|
t.string "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "teachers", :force => true do |t|
t.integer "teacherType_id"
t.string "firstName"
t.string "lastName"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "teachers", ["teacherType_id"], :name => "index_teachers_on_teacherType_id"
teacher_typ.rb
文件如下所示:
class TeacherType < ActiveRecord::Base
has_many :teachers
attr_accessible :title, :teacher_type_id (Also tried :teacherType_id)
end
我的teacher.rb
文件如下所示:
class Teacher < ActiveRecord::Base
has_one :teacherType
attr_accessible :firstName, :lastName
end
但是,现在当我转到我的 localhost:3000/teacher/new
并尝试使用“1”或“Tutor”作为 TeacherType 创建一个新教师时,但是当我提交形式,我总是得到同样的错误,即:
ActiveModel::MassAssignmentSecurity::TeachersController#create 中的错误
Can't mass-assign protected attributes: teacherType_id
Rails.root: /Users/omar/rails_projects/attendance
Application Trace | Framework Trace | Full Trace
app/controllers/teachers_controller.rb:43:in `new'
app/controllers/teachers_controller.rb:43:in `create'
请求
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"NEJf3bISJsidStVyfdRns0oZ7JzSZ8RqqZSAWgL9hz8=",
"teacher"=>{"teacherType_id"=>"Tutor",
"firstName"=>"Jack",
"lastName"=>"Sparrow"},
"commit"=>"Create Teacher"}`
知道为什么会这样吗?我查看了 attr_accessible
但仍然无济于事
最佳答案
teacherType_id
在 TeachersController#new View 中被分配了一个字符串,但它被声明为一个整数。检查你的观点。另外,我会避免使用大小写混合的名称,例如 teacherType_id
...
关于ruby-on-rails - 无法批量分配 protected 属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15137173/