Rails 4 how to make enrolment to a course for users (controller, view, model) -
i trying build simple learning app rails , facing problems. when user signs course try make viewed_course record them can display viewed_course on profile page.
i can't figure out how set different controllers creates viewed_record when on course controller /index.
how can modify course/index view creates new viewed record current signed in user.
i using devise.
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
thank in advance helping me out!
something should it, in courses controller show action (index action should list of courses, show action should viewing individual course):
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
Comments
Post a Comment