0

と の 2 つのモデルがStudyありStudyTypeます。後者には 2 つの列しかありません。 idandと は、 BachelorMasternameなどの研究の種類を格納します。すべての値はすでに挿入されています。に適切なステートメントを追加した後、コマンドを使用しました。およびとして の場所。rake db:seed/db/seeds.rb
StudyType has_many :studiesStudy belongs_to :study_typeaccepts_nested_attributes_for :study_type

ここで、new-study-form に選択フィールドが必要です。選択フィールドを作成しましたが (以下を参照)、フォームを送信すると、フォームはテーブルに新しいエントリを挿入しstudy_typesます?!? オブジェクトidstudy_typeを設定する代わりに。study手伝って頂けますか?

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 に答える