I'm building a training website where I have two models, User
and Course
, that are associated with a third model, CourseCompletions
. The third model is for keeping track of which user has completed which courses and vice versa. The first two models have controllers whereas the third one does not.
I implemented the functionality for completing a course and it works (clicking the "complete course" button on the course page inserts the appropriate row into the course_completion table if the user has not completed that course before), but I'm unsure about how robust and secure my implementation is. This is in Course_Controller.rb:
helper methods omitted for brevity
def complete_course
@course = current_course
@user = current_user
if !already_completed
@course.course_completions.create(user_id: @user.id, course_id: @course.id, completion_date: Time.now)
flash[:success] = "Congratulations! Your progress has been saved."
redirect_to course_path
else
flash[:success] = "Looks like you have already completed this course before, but mad props for reviewing it!"
redirect_to course_path
end
end
My questions are as follows:
- Should I be calling create like I am doing, or is build (or create!) a better option?
- Should I be using strong parameters inside that function? If so, how do I do that in this particular case?
Thank you in advance.