と の 2 つのモデルがStudy
ありStudyType
ます。後者には 2 つの列しかありません。 id
andと は、 Bachelor、Mastername
などの研究の種類を格納します。すべての値はすでに挿入されています。に適切なステートメントを追加した後、コマンドを使用しました。およびとして
の場所。rake db:seed
/db/seeds.rb
StudyType
has_many :studies
Study
belongs_to :study_type
accepts_nested_attributes_for :study_type
ここで、new-study-form に選択フィールドが必要です。選択フィールドを作成しましたが (以下を参照)、フォームを送信すると、フォームはテーブルに新しいエントリを挿入しstudy_types
ます?!? オブジェクトid
のstudy_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." %>