0

我有两个模型:StudyStudyType. 后者只有两列:idname存储研究的类型,如本科硕士等。所有值都已插入。我rake db:seed在将适当的语句添加到/db/seeds.rb.
StudyType has_many :studieswhereStudy belongs_to :study_typeaccepts_nested_attributes_for :study_type

现在我想在我的新学习表格中选择一个字段。我创建了选择字段(见下文),但是当我提交表单时,表单会在表插入一个新条目study_types?!?而不是在对象中设置idof 。你能帮助我吗?study_typestudy

下面是new动作studies_controller

@study = Study.new
...
@study.study_type = StudyType.new

这是研究的部分内容:

<%= render :partial => "shared/study_form_fields", :locals => { :study_fields => study_fields } %>  
...
<%= study_fields.fields_for :subject_type do |study_type_fields| %>
  <%= render :partial => "shared/study_type_form_fields", :locals => { :study_type_fields => study_type_fields } %>
<% end %><!-- End of study_type_fields -->
...

这是 study_type 部分。

# File: _study_type_form_fields.html.erb
<%= study_type_fields.label :name, "Study type" %>
<%= study_type_fields.collection_select :name, StudyType.all, :id, :name, :prompt => "Please select a study type." %>
4

1 回答 1

1

我应该你需要使用study_type_id而不是name这里

# File: _study_type_form_fields.html.erb
<%= study_type_fields.label :study_type_id, "Study type" %>
<%= study_type_fields.collection_select :study_type_id, StudyType.all, :id, :name, :prompt => "Please select a study type." %>
于 2011-03-26T19:27:46.383 回答