Rails how to make this view work? Devise, Enrolment system with courses -


how can modify view make work? have "enroll button" create viewed_lessons record current_user?

class course     has_many :lessons end  class lesson   #fields: course_id   belongs_to :course end class user   has_many :viewed_lessons   has_many :viewed_courses end class viewedlesson   #fields: user_id, lesson_id, completed(boolean)     belongs_to :user   belongs_to :lesson end class viewedcourse   #fields: user_id, course_id, completed(boolean)     belongs_to :user   belongs_to :course   end    class coursescontroller < applicationcontroller   def index     @courses = course.all   end    def show     @course = course.find(params[:id])     current_user.viewed_courses.create(course: @course)   end   end 

one gotcha create new viewed course every time viewed page, need logic add each course once.

perhaps this:

def show   @course = course.find(params[:id])   current_user.viewed_courses.create(course: @course) unless viewedcourse.exists?(user_id: current_user.id, course_id: @course.id) end 

thank helping me out.

in model, can validate uniqueness...

viewed_course.rb

class viewedcourse   ...   validates :course, uniqueness: { scope: :user, message: "should happen once per user" }   ... end 

it doesn't you're using viewedcourses in show template. if later, might want try the find_or_create_by method in instances.

one final piece of unsolicited feedback, verbiage seems little confusing me. "enroll" , "show" feel 2 different actions.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -