Rockwell Rice
Rockwell Rice

Reputation: 3002

Ruby Quiz setup

Got the answer in the comments in the first answer listed, it was verifying all along, I just had to add the flash to the view.

How can I alter this code to make it verify the answer is correct ?

I followed this tutorial http://quizzsystem.comyr.com/web-page/

what it is supposed to do is run check (/quizzs_controller.rb) on an answer to verify if it is correct or not

/quizzs_controller.rb

class QuizzsController < ApplicationController
  before_action :set_quizz, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate, :except=>[:home, :answering, :answer, :check]

 ...

 def check
   @quizz = Quizz.find(params[:id])
   respond_to do |format|
     if params[:ans][0][email protected]
      flash[:notice] = "<b>Congratulation. You gave the correct answer to the question: " + @quizz.question + "</b>"  
       format.html { redirect_to({:controller => "quizzs", :action =>  "answering",:id=>"1" } ) }
      format.xml { head :ok }
    else
      flash[:notice] = "<b p style='color: red'>I am sorry but that is not the right answer to the question: " + @quizz.question + "</b>"
      format.html { redirect_to({:controller => "quizzs", :action =>   "answering",:id=>"1" } ) }
      format.xml { head :ok }
    end 
  end
 end

...

So when you hit the button in the view below it runs 'check' to verify the answer, display the corresponding message on the page it redirects to which is /answering.html.erb.

/answer.html.erb

*I added the "=" sign before form_tag, that is the only change from the original code (unless there is a typo I still have not caught)

   ...


  <%= form_tag( :action => "check",:id => @quizz.id) do %>
<p>
  <b>The correct answer is number: </b>
   <%= text_field :ans,params[:ans]%>
 </p>

<p><%= submit_tag("check")%></P>

    <%end%>

<%= link_to 'Back', {:controller => "quizzs", :action => "answering",:id=>"1" } %>

...

render back to this page and display the message

/answering.html.erb

<h2>Which question do you want to answer</h2>
<table>
<tr>
    <th>Question</th>
</tr>
<%@quizzs.each do |quizz|%>
<tr>
    <td><%=h quizz.question %></td>
    <td><%= link_to '<> Answer this', :controller => "quizzs", :action => "answer", :id =>quizz.id%></td>
</tr>
<%end%>
</table>
<br />

But instead what happens is it seems to look for a route called 'check' instead of verifying whether or not the question is correct.

I hope that is a better presentation of my question, I am still new to this stuff.

Upvotes: 0

Views: 171

Answers (2)

Raghavan Kandala
Raghavan Kandala

Reputation: 299

resources :quizzs will only generate routes for Create/Read/Update/Destroy actions. For any custom actions in the controller, a new route entry needs to be added to the routes.rb file. one way to do this is to declare the new action under the resources block like this.

resources :quizzs do
   member do
     patch 'check'
   end
end

If you run rake routes from your shell, you should see a line which will have quizzs#check. Any route which is not present in the output of rake routes is not known to Rails.

Hope this helps solve the problem and helps you understand how routing works.

Upvotes: 1

rlecaro2
rlecaro2

Reputation: 755

Your "check" action isn't routed. Below resources :quizzs add

patch 'quizzs/check/:id.:format' to: 'quizzs#check' #this names might be different

I hope this solves your issue.

PD: You can always use rake routes in your terminal to check the routes you have defined and their methods.

GL & HF.

Upvotes: 0

Related Questions