嵌套表单中的Rails复选框

时间:2013-01-24 18:05:41

标签: ruby-on-rails ruby-on-rails-3.2 rails-activerecord

我正在尝试以嵌套的形式实现一个复选框。

我有3个模特:科目,课程和小组。 A科目有很多课程,每节课都属于许多小组。

以另一种方式表达: 我有主题作为父类,课程作为子类,作为“孙子”类。

在“主题”表单中,我允许以嵌套形式创建3个课程。 我已经预先创建了组,现在我想在每节课中为组创建HABTM复选框,但是我在尝试在Rails中发送参数时遇到了一些麻烦。

参数的主要区别在于我目前正在发送

"lesson"=>{"group_ids"=>["105", "106", "107"]} 

自己。但是我想要的是为这个主题的每个单独的课程发送group_ids:

"lessons_attributes"=>{"0"=>{"lesson_type"=>"Lecture", 
                             "id"=>"348",
                             "group_ids"=>["37", "38", "39", "40", "41", "42", "131"]}.

详细说明, 这是我发送的当前参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"7r9g2d4lmiSyNZ60i8uos9m1shxVwb0Ly23Tkrshv8w=", "subject"=>{"subject_code"=>"", "subject_name"=>"", 
                   "lessons_attributes"=>{"0"=>{"lesson_type"=>"Lecture", "id"=>"348"}, 
                                          "1"=>{"lesson_type"=>"Tutorial","id"=>"349"}, 
                                          "2"=>{"lesson_type"=>"Laboratory","id"=>"350"}}, 
                                          "remarks"=>""},
                                          "lesson"=>{"group_ids"=>["105", "106", "107"]}, "commit"=>"Update Subject", "id"=>"166"}

我想发送的参数是这样的:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"7r9g2d4lmiSyNZ60i8uos9m1shxVwb0Ly23Tkrshv8w=", "subject"=>{"subject_code"=>"", "subject_name"=>"", 
                 "lessons_attributes"=>{"0"=>{"lesson_type"=>"Lecture", "id"=>"348","group_ids"=>["37", "38", "39", "40", "41", "42", "131"]}, 
                                        "1"=>{"lesson_type"=>"Tutorial","id"=>"349", 
                                              "group_ids"=>["37", "38", "39", "40", "41", "42", "131"], 
                                        "2"=>{"lesson_type"=>"Laboratory","id"=>"350",
                                              "group_ids"=>["37", "38", "39", "40", "41", "42", "131"]}}, "remarks"=>""}, "commit"=>"Update Subject", "id"=>"166"}

我没有正确发送参数,问题出在:`

<%= check_box_tag "lesson[group_ids][]", group.id, f.object.groups.include?(group) , id: "lesson_group_ids_#{group.id}"%>`

特别是对于我的复选框,我正在实施此功能:课程[group_ids] []

我也尝试过lesson_attributes [group_ids] []。 但同样地,我无法获得HABTM关系。我不知道我能做什么,以便小组为每节课保存。任何帮助将不胜感激。

以下是相关代码:

<%= form_for(@subject) do |f| %>

  <div class="field">
    <%= f.label :subject_name %><br />
    <%= f.text_field :subject_name %>
  </div>

  <div>
    <%= f.fields_for :lessons do |lesson| %>
     <%= render "lesson_fields", :f => lesson %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>

在_lesson_fields.html.erb中,我有:

<div class="field">
    <%= f.label :lesson_type %><br />
    <%= f.text_field :lesson_type,:readonly => true %>
  </div>

  <div>
  <% Group.all.each do |group|%>
   <div>
      <%= check_box_tag "lesson[group_ids][]", group.id, f.object.groups.include?(group) , id: "lesson_group_ids_#{group.id}"%>       
     <%= group.group_index %>  
   </div>
  <%end%>
  </div>

对于模型

相关模型:

class Subject < ActiveRecord::Base
  attr_accessible :lessons_attributes
  has_many :lessons
end

class Lesson < ActiveRecord::Base
  belongs_to :subject
  has_and_belongs_to_many :groups
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :lessons 
end

1 个答案:

答案 0 :(得分:0)

我相信你在复选框标签的名称'课程'之后缺少一个索引(如'lesson [0] [group_ids] []'。你可以手动添加,或者你可以使用fields_for做一些事情。我倾向于做你正在做的事情 - 循环遍历所有组并显示所有组,决定是否用布尔测试检查它们。