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)