Closed jamiesonreinhard closed 3 years ago
Yes, @jamiesonreinhard. I stopped at the router level, but it needs the controller.
It's the same old flow of Router, Controller, View.
The job of this controller action will be to take the parameters from the form (the users answers to the quiz) and initialize the session with them in order for the report to be rendered.
You can check this controller for reference: Course::Quiz::SessionsController
.
The job of this controller action will be to take the parameters from the form (the users answers to the quiz) and initialize the session with them in order for the report to be rendered.
You can check this controller for reference:
Course::Quiz::SessionsController
.
I think I'm on the right track here but maybe you can give me a hint... not sure how to intantiate an instance of a quiz properly here in the highlighted line. @caioertai
So. 2 things:
#build
from the quiz builder, for consistency. #new_quiz
should have been a private method. My bad there. Quiz#session
. No need to instantiate it yourself.Ignore my second point. I just realized this only works for a new session. It might be a good idea to include the parameters on the method to make it better in the future
Got it. So now the quiz builder "build" method is breaking because it isn't receiving a "quizable" with params. The "quizable" should be the article in this case, right? So how can I pass the article from the homepage view, to the guest session controller, to the quiz builder?
Alright. Let's rewind.
Here's the Course::Quiz::SessionsController#create
def create
@quiz = Quiz.find(params[:quiz_id])
@quiz_session = Quiz::Session.new(quiz_session_params.merge(quiz: @quiz))
authorize @quiz_session
if @quiz_session.save
redirect_to course_section_path(@quiz.quizable, anchor: "quiz")
else
@section = @quiz_session.quiz.quizable
render "course/sections/show"
end
end
It needs an instance of a quiz, then it can pass it, along with the quiz session params to the session.
For the the course action the quiz can be found via id, since it's persisted. In case of the guest section, we're going to need to build it again, and the way to build it is by using the QuizBuilder
. You have the right idea using it, but you're not using its interface properly. Check the initialize:
It needs a user, which is a current_user (a guest in this case), and a quizable (the article).
Indeed getting the article in the create is not possible right now since we don't have the article being sent as an argument or present in the body of the POST. We have 2 ways for solving this (3 with a hack):
resources :article
as part of the post route (and change the form accordingly to include quiz.quizable
). So the form build would become: [:guest, quiz_session.quiz.quizable, quiz_session]
.@quizable
in the controller as Article.first
.f.input :article_id, as: :hidden, value: quiz_session.quiz.quizable.id
to the form.Thanks for the explanation there. That makes sense now. I think I'm getting closer, let me know if this looks correct.
This is the new error related to not finding a quiz question:
Yeah.... this one might be tricky. Can we have a pair session tomorrow about it, @jamiesonreinhard? I'm concerned and involves some other things I wasn't planning on.
I don't have a single meeting tomorrow, so any time should be fine.
Ok great, how about 11am CT?
Ok great, how about 11am CT?
Perfect.
@caioertai I worked a little bit on styling the intro quiz report show page, as well as the actual quiz on the homepage. I made the quiz expandable/collapsible which I think looks okay - I think it could use more styling in the future but works well for now. Let me know what you think.
@caioertai also, there are two bugs I found.
@caioertai also, there is one bug. When you're on the show page for the quiz report and you try to refresh, it breaks. Thoughts on a workaround for that?
Well... we can leave this for another time. We could save the quiz results in the user session (cookies), so if the user refreshes we check for a saved quiz session in the session (yeah... sounds weird, I know)...
Btw. It's not a bug. There IS no quiz session to show unless it comes straight from the form the user submits. So it only lasts one request. It's like this:
User answers form, and clicks submit
A POST request with the body
having the answers is created.
Controller processes the request and renders show.
Effectively, you're looking at the rendered results of a POST request at /sessions/show
Now... if you refresh this page, you're just trying to GET /sessions/show
which doesn't exist.
So what we could do on a GET is check the user cookies for a quiz session.
Closes #93
Reviewed mentor branch and implemented changes with several commits on this branch.
Next step is to render a quiz report upon submitting the intro-quiz on the homepage. I'm trying to wrap my head around this logic.
@caioertai -- will I need to create a new Guest::Sessions controller for this? See attached routing error.