简体   繁体   English

带有嵌套属性的simple_form

[英]simple_form with Nested Attributes

I think I am going to ask a 2-part question; 我想我会问一个由两部分组成的问题; if this should be separated into 2 questions, my apologies. 如果这应该分成两个问题,我道歉。

First off, I am trying to store courses into a course table (course_name, instructor, start_time, end_time, etc...) which would have an ActiveRecord relation with course_days ( course_id and day which corresponds with the day of the week => 1 for Sunday, 2 for Monday, etc...). 首先,我试图将课程存储到course表(course_name,instructor,start_time,end_time等等)中,这些course_dayscourse_days有一个ActiveRecord关系( course_idday与星期几相对应=> 1周日,周一2,等等......) My end goal is to display these as a schedule in FullCalendar, which is why I think separating the course and course_days into two tables with an ActiveRecord relation would be best. 我的最终目标是在FullCalendar中将这些作为时间表显示,这就是为什么我认为将coursecourse_days分成两个具有ActiveRecord关系的表是最好的。 Using Rails, is this a good way to achieve this? 使用Rails,这是实现这一目标的好方法吗?

If so, secondly, I'm using Simple Form to add the following data to 2 tables: course and course_days . 如果是这样,其次,我使用Simple Form将以下数据添加到2个表: coursecourse_days And I think I am getting close. 我想我越来越近了。 I am just having some difficulty adding the course_days (eg, 3 separate rows will be added to course_days if "Monday", "Tuesday" and "Thursday" are checked. 我只是在添加course_days时遇到一些困难(例如,如果检查“星期一”,“星期二”和“星期四”,则会在course_days添加3个单独的行。

Here is what I am currently working with: 以下是我目前正在使用的内容:

#new.html.erb
<%= simple_form_for @course, html: { autocomplete: 'off' } do |f| %>
<%= f.input :title, label: 'Class Name', placeholder: 'Class Name' %>
<%= f.input :instructor, placeholder: "Instructor Name" %>
<%= f.input :instructor_email, placeholder: 'Instructor Email' %>
<%= f.input :building, placeholder: 'Building' %>
<%= f.input :room, placeholder: 'Room' %>
<%= f.input :semester do %>
    <%= f.select :semester_id, @semesters.map { |s| [s.name, s.id] } %>
<% end %>
<%= f.simple_fields_for :course_days do |p| %>
    <%= p.input :day, :as => :boolean %>
 <% end %>
<%= f.button :submit, :class => 'btn btn-primary' %>
#app/models/course.rb
class Course < ActiveRecord::Base
  has_many :course_days
  accepts_nested_attributes_for :course_days
end

#app/models/course_day.rb
class CourseDay < ActiveRecord::Base
  belongs_to :course
end

#app/controllers/courses_controller.rb

  def new
   @course = Course.new
   7.times  { @course.course_days.build } #For 7 days of the week??
   @semesters = Semester.all() 
  end

 def create
   @course = Course.new(course_params)
   if @course.save
     redirect_to action: 'new'
     flash[:notice] = "Course Created."
   else
     render :new
   end
 end

 def course_params
  params.require(:course).permit!
 end

Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT: The issues I am having with the form above: 1) How do I generate 7 checkboxes with an assigned attribute to determine if its a Monday, Tuesday, etc? 编辑:我在上面的表格中遇到的问题:1)如何生成带有指定属性的7个复选框,以确定它是星期一,星期二等? 2) Using the nested attributes, it is only submitting one record to the database, even if multiple checkboxes are checked. 2)使用嵌套属性,即使检查了多个复选框,也只向数据库提交一条记录。

Edit 2 编辑2

So, essentially, when that form is submitted, I would like the database tables to look something like this: 所以,基本上,当提交该表单时,我希望数据库表看起来像这样:

  courses: id => 14, name => Bio 101, start_time => 08:00:00, end_time 09:00:00

  course_days: id => 11, course_id => 14, day => 1  (For monday)
  course_days: id => 12, course_id => 14, day => 3  (For wednesday)
  course_days: id => 13, course_id => 14, day => 5  (For friday)

Please take into account that having another table, will need to fetch that rows from the DB. 请考虑具有另一个表,需要从数据库中获取该行。

So when you ask for a course, you will need a maximum of 7 more hits for knowing the course days. 因此,当您要求课程时,您需要最多7次点击才能了解课程日期。

I suggest that you have a days column in the Course model. 我建议您课程模型中有一个days列。 This column will save the selected days in comma separated values. 此列将以逗号分隔的值保存选定的天数。

This way you don't need accepts_nested_attributes_for . 这样您就不需要accepts_nested_attributes_for In this case, what you need to do is to populate the Course.days columns with the checkboxes values using a before_validation callback. 在这种情况下,您需要做的是使用before_validation回调使用复选框值填充Course.days列。

I am not sure, but what about: 我不确定,但是怎么样:

   7.times  { |i| @course.course_days.build day: i } #For 7 days of the week??

This way you would have 7 instances with different values of days. 这样,您将拥有7个具有不同天数值的实例。 You could use i+1 to start in 1. 您可以使用i+1从1开始。

I suppose this would send something like: 我想这会发送类似的东西:

{
  "course" => {
    "course_days_attributes" =>{
      "0" => { "day" => "1"},
      "1" => { "day" => "5"},
    }
  }
}

Try this just for testing, then you can refactor: 试试这只是为了测试,然后你可以重构:

<%= f.simple_fields_for :course_days do |p| %>
  <%= p.input :day, :as => :check_box, input_html: { value: f.object.course_days.day } %>
<% end %>

I don't know if you can use p.object.day instead of f.object.course_days.day . 我不知道你是否可以使用p.object.day而不是f.object.course_days.day This should be automatic though. 这应该是自动的。

Another thing I would try would be this: 我会尝试的另一件事是:

<%= f.simple_fields_for :course_days, f.object.course_days do |p| %>

But actually I can't explain what exactly is happening. 但实际上我无法解释究竟发生了什么。

I am not sure about this though. 我不确定这一点。

First off, simple_form has collections for inputs 首先,simple_form具有输入集合

So 所以

<%= f.input :semester do %>
  <%= f.select :semester_id, @semesters.map { |s| [s.name, s.id] } %>
<% end %>

you can write as 你可以写作

<%= f.input :semester, collection: @semesters %>

Second, to get days as boolean you can do something like this: 其次,要将天数作为布尔值,您可以执行以下操作:

<%= f.simple_fields_for :course_days do |p| %>
  <%= p.input :day, collection: [['Monday',1],['Tuesday',2][so forth]], as: :check_boxes %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM