編集 - 修正済み:問題はparamsハッシュであり、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
and : this_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
andを使用cocoon
して、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"=>""}}
これらの生徒の属性はどこに移動する必要がありますか/新しい生徒を作成するには、どのように処理する必要がありますか? ありがとう!