我正在尝试使用Project表单来构建几个(最多12个)志愿时间块的第一个(开始)时间。
project.rb
class Project < ActiveRecord::Base
attr_accessible :title, ...
has_many :vol_times, :dependent => destroy
accepts_nested_attributes_for :vol_times, :reject_if => lambda { |a| a[:start_time].blank? }, :allow_destroy => true
...
end
vol_time.rb
class Vol_time < ActiveRecord::Base
attr_accessible :start_time, ...
belongs_to :project
end
ProjectsController
class ProjectsController < ApplicationController
before_filter :signed_in_user, only: :create
...
def new
@project = Project.new
@user = current_user
@project.vol_times.build
end
...
end
Vol_Times Controller
class Vol_TimesController < ApplicationController
def new
@reward = Reward.new
end
...
end
我的观点看起来像这样......
<%= form_for(@project) do |f| %>
<div class="form_field_block">
<p class="form_label"> Project Title</p>
<%= f.text_field :title, :size => 40, :placeholder => " Project Title...", :class => "frm" %>
</div>
<div class="form_field_block">
<p class="form_label"> Project Sub-title</p>
<%= f.text_field :sub_title, :size => 40, :placeholder => " Project Sub-title...", :class => "frm" %>
</div>
<p class="clearing"></p>
<div class="form_field_block">
<% f.fields_for :vol_times do |builder| %>
<%= render :partial => 'start_time', :f => builder %>
<% end %>
</div>
<p class="clearing"></p>
<%= button_tag "btn_start_project.png", :class => "btn_save" %>
<% end %>
而_partial
看起来像这样......
<%= f.label :start_time, "Starting Time" %>
<%= f.text_field :start_time %>
当我查看页面时,我看到包含<div>
,但不是ERB的内容,应该从_partial
解析。
为什么这不起作用的任何想法?我从Ryan Bates的RailsCast#196 - Here
获得了一般背景答案 0 :(得分:1)
您错过了=
上的fields_for
。它应该是
<%= f.fields_for :vol_times do |builder| %>