0

这是Can't Mass Assign Protected Attributes Error的后续问题

我正在尝试使用 Colleciton_Select 以便我可以从下拉框中选择 TeacherType,而不是允许用户写入teacherType_id。

<%= collection_select(:teacherType, :teacherType_id, TeacherType.order('title'), :id, :title, :prompt => true) %>

但是,当我从下拉菜单中选择一个选项时,它总是说教师已成功更新,但教师模型没有任何变化。

难道我做错了什么?

4

1 回答 1

1

首先,我认为您正在尝试添加teacherTypetoTeacher而不是,TeacherType因此您应该使用

<%= collection_select(:teacher, :teacherType_id, TeacherType.order('title'), :id, :title, :prompt => true) %>

现在,我想提到的第二点,如果您has_many在一个模型中具有关联,那么您应该始终在另一个模型中具有belongs_to关联。

所以修改Teacher模型中的关联定义

 has_one :teacherType

 belongs_to :teacherType

还有一点,将关联名称作为关联模型的下划线复数形式是一种很好的做法。Convention over configuration is the way rails applications are supposed to be built.如果在任何情况下您都不能拥有此名称,那么您必须class_name为关联定义指定属性。

于 2013-03-02T14:18:51.753 回答