Ruby on rails 无法通过嵌套表单提交复选框
我正在尝试创建一个应用程序,让老师们每天选择不在学校的学生。我通过漂亮的生成器gem创建了模型。问题是它不会提交到notpresents表。请帮忙Ruby on rails 无法通过嵌套表单提交复选框,ruby-on-rails,nested-attributes,form-for,Ruby On Rails,Nested Attributes,Form For,我正在尝试创建一个应用程序,让老师们每天选择不在学校的学生。我通过漂亮的生成器gem创建了模型。问题是它不会提交到notpresents表。请帮忙 # == Schema Information # # Table name: students # # id :integer not null, primary key # name :string(255) # group_id :integer # created_at :dateti
# == Schema Information
#
# Table name: students
#
# id :integer not null, primary key
# name :string(255)
# group_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Student < ActiveRecord::Base
attr_accessible :name, :group_id
belongs_to :days
end
# == Schema Information
#
# Table name: notpresents
#
# id :integer not null, primary key
# student_id :integer
# day_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class Notpresent < ActiveRecord::Base
attr_accessible :student_id, :day_id
belongs_to :days
end
# == Schema Information
#
# Table name: days
#
# id :integer not null, primary key
# title :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class Day < ActiveRecord::Base
attr_accessible :title, :presents
has_many :notpresents
accepts_nested_attributes_for :notpresents
end
#==架构信息
#
#表名:学生
#
#id:整数不为空,主键
#名称:字符串(255)
#组id:整数
#创建时间:datetime非空
#更新时间:datetime非空
#
班级学生
和查看_form.html.erb
<%= form_for @day do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<% for student in Student.find(:all) %>
<div>
<%= check_box_tag :notpresents, student.id%>
<%= student.name %>
</div>
<% end %>
<p><%= f.submit %></p>
<% end %>
我从未使用过漂亮的生成器宝石,但是如果一个学生可能会缺席很多天,而一天可能会有很多学生缺席,那么你不应该有多对多的关系吗
class Student < ActiveRecord::Base
attr_accessible :name
has_many :days, through: :notpresents
has_many :notpresent
end
class Days < ActiveRecord::Base
attr_accessible :date
has_many :students, through: :notpresents
has_many :notpresent
end
class :Notpresents < ActiveRecord::Base
attr_accessible :student_id, :day_id
belongs_to :students
belongs_to :days
end
app/views/days/_form.html.erb:
<%= simple_form_for @day do |f| %>
<%= f.association :students, as: :check_boxes %>
<% end %>
您需要在此处使用嵌套属性。请仔细阅读:并遵循。我可以建议将Notpresent更名为缺席吗?@SyedAslam我尝试了neste railscast修订版,但没有成功。我可以得到正确的参数,但它不会传递到正确的表。@ilikepie是的!这只是一周工作的最后一个小时大脑在工作…:我最终得到了这个解决方案。我所拥有的唯一一个truble是simple_表单为关联生成的输入和标签的样式和包装。
<%= simple_form_for @day do |f| %>
<%= f.association :students, as: :check_boxes %>
<% end %>