Reputation: 6805
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)
Upvotes: 3
Views: 3661
Reputation: 525
First off, simple_form has collections for inputs
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 %>
Upvotes: 0
Reputation: 1631
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.
I suggest that you have a days
column in the Course model. This column will save the selected days in comma separated values.
This way you don't need 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.
Upvotes: 2
Reputation: 21785
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. You could use i+1
to start in 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
. 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.
Upvotes: 0