我正在尝试创建一个teacher
表,其中有一个名为“teacher_type_id”的列,即它是一个外键,连接到teacherType
具有三行的表,即 1 => Tutor、2 => Module Leader 和 3 => Lecturer .
我的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”作为教师类型创建一个新教师时,但是当我提交表单时,我总是得到相同的错误,即:
ActiveModel::MassAssignmentSecurity::Error in 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'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"NEJf3bISJsidStVyfdRns0oZ7JzSZ8RqqZSAWgL9hz8=",
"teacher"=>{"teacherType_id"=>"Tutor",
"firstName"=>"Jack",
"lastName"=>"Sparrow"},
"commit"=>"Create Teacher"}`
任何想法为什么会这样?我看了一下,attr_accessible
但仍然无济于事