我正在跟随这个Rails 教程,描述如何做嵌套模型表单。在 4:32,他开始描述如何用三个空模型预填充表单。有问题的两个模型是:
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
)很多次,但没有任何效果。我究竟做错了什么?