我正在尝试创建一个teacher表,其中有一个名为“teacher_type_id”的列,即它是一个外键,连接到teacherType表,该表有三行,即1=>Tutor、2=>Module Leader和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”作为教师类型创建一个新教师时,但是当我提交表单时,总是会得到相同的错误,即:
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但仍然没有结果

最佳答案

教师:

attr_accessible :firstName, :lastName, :teacherType_id

09-16 00:01