控制器: project_sub_types_controller.rb
def new
@svn_repos = ['svn_software','svn_hardware']
@project_sub_type = ProjectSubType.new
end
型号: project_sub_type.rb
class ProjectSubType < ActiveRecord::Base
belongs_to :project_type
has_many :repositories, :dependent => :destroy
accepts_nested_attributes_for :repositories
end
查看: _form.html.erb
<%= form_for @project_sub_type, :html => {:class => 'project_subtype_form'} do |f| %>
<%= f.label :name, "Project sub type name" %>
<%= f.text_field :name %>
<%= f.fields_for :repositories do |ff| %>
<%= ff.label :select_svn_repositories, "Select SVN repositories" %>
<% @svn_repos.each do |repos| %>
<%= ff.check_box :repos_name, {}, "#{repos}", nil %>
<%= h repos -%>
<% end %>
<%= f.submit "Save"%>
问题: fields_for :repositories
部分未显示在前端。任何人都可以指出我的错误是什么?以下:此link
答案 0 :(得分:2)
用于创建表单的@project_sub_type
没有任何关联的存储库,因此表单上没有嵌套字段。
如果添加一个(例如,在控制器中执行@project_sub_type.repositories.build
),您将看到f.fields_for
语句生成的一组空白字段。