0

My models

  class Course
       has_many :lessons, through: :chapters
end

class Lesson
  belongs_to :chapter
  has_one :course, through: :chapter
end

class User
  has_many :user_lessons
end

class UserLesson
   #fields: user_id, lesson_id, completed(boolean)  
   belongs_to :user
   belongs_to :lesson
end

class Chapter 
   has_many :lessons
   belongs_to :course
end 

My UserLesson Controller:

 class UserLessonsController < ApplicationController
   before_filter :set_user_and_lesson
   def create
     @user_lesson = UserLession.create(user_id: @user.id, lession_id:    @lesson.id, completed: true)
if @user_lesson.save
  # redirect_to Course show action///// How can I redirect to that?
else
  # take the appropriate action
end
  end
 end

Basically what I want to to is to redirect_to the show action of the Course that has the lesson? (it is basically a go back to the whole list of lessons. Lessons are nested within Course) The problem is that could not find lecture id.

No route matches {:action=>"show", :controller=>"lectures", :id=>nil}    missing required keys: [:id]

Routes.rb

  resources :courses do 

      resources :lessons

  end

   resources :user_lessons

Links that send the data to create method

   <%= link_to 'Back to Lesson list', user_lessons_path(@user_lesson, user_lesson: {user_id: 'current_user.id', lesson_id: 'lesson.id'}), :method => :post, data: {confirm: 'are you sure?'} %>
AdrienNhm
  • 165
  • 1
  • 10

3 Answers3

0

Pass the id in your redirect_to method:

redirect_to :controller=>'courses', :action => 'show', :id => @user_lesson.lesson.course.id

Update after your question edit:

Change this:

   <%= link_to 'Back to Lesson list', user_lessons_path(@user_lesson, user_lesson: {user_id: 'current_user.id', lesson_id: 'lesson.id'}), :method => :post, data: {confirm: 'are you sure?'} %>

to:

   <%= link_to 'Back to Lesson list', user_lessons_path(@user_lesson, user_lesson: {user_id: current_user.id, lesson_id: lesson.id}), :method => :post, data: {confirm: 'are you sure?'} %>

which is basically changing this:

user_lesson: {user_id: 'current_user.id', lesson_id: 'lesson.id'}

to:

user_lesson: {user_id: current_user.id, lesson_id: lesson.id}

see if that works.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • I am actually redirecting to another controller: courses_controller.rb and the problem is that it does not find the course id as it is not passed. – AdrienNhm Aug 18 '15 at 20:37
  • right, so you need to pass the course_id to that controller. – K M Rakibul Islam Aug 18 '15 at 20:38
  • undefined method `course' for nil:NilClass cant seem to understand why – AdrienNhm Aug 18 '15 at 20:50
  • you need: `@user_lesson.lesson.course.id` this should work – K M Rakibul Islam Aug 18 '15 at 20:53
  • I still have the message error. My guess is that the lecture_id is never passed to the controller UserLessons. I edited the post with the link that passes the data to the controller. – AdrienNhm Aug 18 '15 at 21:01
  • did you try my updated answer? I still see ` <%= link_to 'Back to Lesson list', user_lessons_path(@user_lesson, user_lesson: {user_id: 'current_user.id', lesson_id: 'lesson.id'}), :method => :post, data: {confirm: 'are you sure?'} %> ` in your code! – K M Rakibul Islam Aug 18 '15 at 23:01
  • Do you have the project on github? Then, add me as a collaborator and I will take a look after cloning it. In that case, my github id is: rakibulislam – K M Rakibul Islam Aug 19 '15 at 04:19
0
redirect_to lessons_path(@user_lesson)

But you should define show action in your UserLessons controller.

Nikola Todorovic
  • 300
  • 3
  • 12
0

Here's what I would do: modify lesson class

class Lesson
  belongs_to :chapter
  has_one :course, through: :chapter #changed from :lecture since it's nowhere in your model structure
end

then you can use

redirect_to @lesson.course

to redirect to a lesson's course

Hope I got this right :)

Edit: I see that you mentioned lecture in multiple occasions but it's nowhere to be found in what you provided.... so I'm assuming it's now Course ? :)

If that's the case... change this as well

class Chapter 
  has_many :lessons
  belongs_to :course # was lecture
end 
Alexandre Voyer
  • 809
  • 6
  • 11