2013-07-04 74 views
0

編輯 - 固定:問題是params哈希表,並通過大量的做題看後,我想出了以下解決方案,其中(到目前爲止)效果很好:使用繭創建嵌套子模型

student_groups#new行動現在看起來像這樣 - 也許有更好的方法來解析params,但是......這是有效的!

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:我有標記:_destroyattr_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按this

<%= 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 %> 

Ë ND編輯

我使用rails 3.2.13cocoon在我的student_group模型中生成一個嵌套窗體。格式化仍然有點棘手,我想實施咖啡標記來自動更新正確數量的學生,但這些都是我以後可以想到的。目前,主要的問題是,雖然表格顯示並可以填寫/提交沒有錯誤,但新的兒童模型並未創建。

這裏的_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"=>""}} 

應該在哪裏,這些學生屬性去/我應該怎麼對付他們,讓他們創建一個新的學生(S)?謝謝!

+0

你也可以看看'acceptersted_attributes_for',它會爲你處理'students_attributes'參數。 [參考資料](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html) – spullen

+0

啊,其實我已經在我的s_g.rb中,它是'...'的一部分 - 但儘管如此,編輯它也很重要 – dax

回答

0

有關解決方案的詳細信息,請參閱原始帖子頂部的編輯。回答這個問題是關閉的。