3

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...). 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. Using Rails, is this a good way to achieve this?

If so, secondly, I'm using Simple Form to add the following data to 2 tables: course and course_days. And I think I am getting close. I am just having some difficulty adding the course_days (e.g., 3 separate rows will be added to course_days if "Monday", "Tuesday" and "Thursday" are checked.

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? 2) Using the nested attributes, it is only submitting one record to the database, even if multiple checkboxes are checked.

Edit 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)
4

3 回答 3

2

请考虑到有另一个表,将需要从数据库中获取该行。

因此,当您要求一门课程时,您最多需要 7 次点击才能知道课程天数。

我建议你Course 模型中有一个days专栏。此列将以逗号分隔值保存选定的日期。

这样就不需要了accepts_nested_attributes_for。在这种情况下,您需要做的是使用before_validation回调使用复选框值填充 Course.days 列。

于 2013-08-02T16:57:03.420 回答
0

我不确定,但是怎么样:

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

这样,您将拥有 7 个具有不同天数的实例。您可以使用i+1从 1 开始。

我想这会发送类似:

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

试试这个只是为了测试,然后你可以重构:

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

不知道能不能用p.object.day代替f.object.course_days.day。这应该是自动的。

我会尝试的另一件事是:

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

但实际上我无法解释到底发生了什么。

不过我不确定。

于 2013-07-31T01:25:43.510 回答
0

首先,simple_form 有输入集合

所以

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

你可以写成

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

其次,要将天数设为布尔值,您可以执行以下操作:

<%= f.simple_fields_for :course_days do |p| %>
  <%= p.input :day, collection: [['Monday',1],['Tuesday',2][so forth]], as: :check_boxes %>
<% end %>
于 2013-08-05T19:15:48.277 回答