ネストされたモデル フォームの作成方法を説明するこのRails チュートリアルに従っています。4:32 で、彼はフォームに 3 つの空のモデルを事前入力する方法を説明し始めます。問題の 2 つのモデルは次のとおりです。
class Event < ActiveRecord::Base
has_many :positions, dependent: :destroy
accepts_nested_attributes_for :positions
end
と...
class Position < ActiveRecord::Base
belongs_to :event
end
イベント コントローラーで、チュートリアルのコードをnew
メソッドに追加しました。
def new
@event = Event.new
3.times { @event.positions.build }
end
そして、私のイベントのフォーム ビューにもデータが取り込まれます。
<!-- /apps/views/events/_form.html.erb -->
<%= form_for(@event) do |f| %>
<h3>Event Details</h3>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<!-- more fields here -->
<h3>Create positions for the event</h3>
<% f.fields_for :positions do |builder| %>
<p>
<%= builder.label :name %>
<%= builder.text_field :name %>
</p>
<!-- more fields here -->
<% end %>
<!-- more fields here -->
<% end %>
ただし、position
フィールドはフォームに表示されません。rake db:migrated
サーバー ( Ctrl-C
、 ) を何度も再起動しましたrake s
が、何の効果もありませんでした。私は何を間違っていますか?