编辑 - 已修复:问题是参数哈希,在查看了很多关于 SO 的问题后,我想出了以下解决方案,它(到目前为止)效果很好:
现在的student_groups#new
动作看起来像这样——也许有更好的方法来解析参数,但是……这行得通!
def create
@student_group = @user.student_groups.build(params[:student_group])
### https://stackoverflow.com/questions/2610263/how-to-access-nested-params
@params = params[:student_group][:students_attributes]
if @student_group.save
### https://stackoverflow.com/questions/14502508/undefined-method-for-nilnilclass-when-pushing-values-to-an-array
if @params.present?
@params.values.each do |student|
@student_group.students.create(name:"#{student[:name]}", gender: "#{student[:gender]}")
end
end
# new subject path
redirect_to class_path(@student_group), flash: { success: "#{@student_group.name} has been added successfully" }
else
@title = "Create a new group"
flash.now[:error] = "Something's gone wrong. Please try again!"
render 'new'
end
结尾
student_group.rb
: 我必须标记:_destroy
为attr_accessor
class StudentGroup < ActiveRecord::Base
### https://github.com/ryanb/nested_form/issues/222
attr_accessor :_destroy
attr_accessible :name, :number_of_students, :type_of_group, :students_attributes
belongs_to :user
has_one :age, dependent: :destroy
has_many :students, dependent: :destroy
accepts_nested_attributes_for :students, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
...
end
和_groups_form.html.erb
:根据这个添加 child_index
<%= form_for @student_group do |f| %>
<p>
The name of this group is
<span class="field form_field"><%= f.text_field :name %></span>
and it is a/an
<span class="field dropdown"><%= f.select :type_of_group, [["select a group type", ""], "young learners class (0-6)", "primary class (7-12)", "secondary class (13-17)", "adult class (18+)", "children's sport team", "adult's sport team"] %></span>
<!-- <span id="nos" class="field dropdown"><%#= f.select :number_of_students, (0..60) %></span> -->
</p>
<table id="nos_header">
<thead>
<tr>
<th>Student name:</th>
<th>Gender:</th>
</tr>
</thead>
<tbody>
<!-- https://stackoverflow.com/questions/11445831/how-to-submit-multiple-new-items-via-rails-3-2-mass-assignment -->
<%= f.fields_for :students, @student, child_index: @student do |builder| %>
<%= render "student_fields", :f => builder %>
<% end %>
<tr>
<td class="links"><%= link_to_add_association 'add student', f, :students %></td>
</tr>
</tbody>
</table>
<%= f.submit "Submit", :class => 'big_button round unselectable'%>
<% end %>
结束编辑
我正在使用rails 3.2.13
andcocoon
在我的 student_group 模型中制作嵌套表格。格式仍然有点麻烦,我想实现 coffeescript 来自动更新正确的学生人数,但这些是我以后可以弄清楚的。目前,主要问题是虽然表格出现并且可以正确填写/提交,但没有创建新的子模型。
这是_form
部分
<%= form_for @student_group do |f| %>
<p>
The name of this group is
<span class="field form_field"><%= f.text_field :name %></span>
and it is a/an
<span class="field dropdown"><%= f.select :type_of_group, [["select a group type", ""], "young learners class (0-6)", "primary class (7-12)", "secondary class (13-17)", "adult class (18+)", "children's sport team", "adult's sport team"] %></span>
<!-- <span id="nos" class="field dropdown"><%#= f.select :number_of_students, (0..60) %></span> -->
</p>
<table id="nos_header">
<thead>
<tr>
<th>Student name:</th>
<th>Gender:</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :students do |builder| %>
<%= render "student_fields", :f => builder %>
<% end %>
<td class="links"><%= link_to_add_association 'add student', f, :students %></td>
</tbody>
</table>
<%= f.submit "Submit", :class => 'big_button round unselectable'%>
<% end %>
和_student_fields
部分:
<tr class="nested-fields">
<td class="field form_field"><%= f.text_field :name %></td>
<td class="field dropdown"><%= f.select :gender, [["select a gender", ""],'Female', 'Male', 'Transgender'] %></td>
<td><%= link_to_remove_association "remove student", f %></td>
</tr>
当我在浏览器中创建一个新组时,没有创建新学生,我可以在服务器中看到:
Started POST "/student_groups" for 127.0.0.1 at 2013-07-04 14:58:51 +0200
Processing by StudentGroupsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"u/DbVbgMKoT6kWPBKsD0sVNBLVRpFY87E5nZQWK+K9o=", "student_group"=>{"name"=>"test", "type_of_group"=>"young learners class (0-6)", "students_attributes"=>{"1372943007652"=>{"name"=>"Johnny Test", "gender"=>"Male", "_destroy"=>""}, "1372943009403"=>{"name"=>"Quizzy", "gender"=>"Transgender", "_destroy"=>""}}}, "commit"=>"Submit"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 56 LIMIT 1
(0.1ms) begin transaction
SQL (0.6ms) INSERT INTO "student_groups" ("created_at", "name", "number_of_students", "type_of_group", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Thu, 04 Jul 2013 12:58:51 UTC +00:00], ["name", "Test"], ["number_of_students", nil], ["type_of_group", "adult class (18+)"], ["updated_at", Thu, 04 Jul 2013 12:58:51 UTC +00:00], ["user_id", 56]]
(2.4ms) commit transaction
Redirected to http://localhost:3000/class/407
Completed 302 Found in 8ms (ActiveRecord: 3.3ms)
这似乎是问题所在 -
"students_attributes"=>{"1372943007652"=>{"name"=>"Johnny Test", "gender"=>"Male", "_destroy"=>""}, "1372943009403"=>{"name"=>"Quizzy", "gender"=>"Transgender", "_destroy"=>""}}
这些学生属性应该去哪里/我应该如何处理它们以便他们创建一个新学生?谢谢!