Topic: Need Help with my Application to use JQuery Rails
Hi I have a problem with my Rails Application currently I am trying to do ajax processing using Rails.
My Model (Question) needs to do a few things
It needs to show a question to the user then when the user click submit it will update the question and show the next question.
It works fine when I was using Rails only but it doesn't work when I tried to include Ajax JQuery.
I am facing 2 Problem
1)The first time when the form is submitted it does not load the next question, only subsequent click loads it
2)I need to redirect the user to another page when all questions are completed which I don't know where to insert the code in.
questions_controller.rb
class QuestionsController < ApplicationController
layout "application"
before_filter :authenticate_student!
def index
current_student.starttime
if current_student.challenge.completed?
redirect_to student_result_path
elsif current_student.challenge_start==false
redirect_to student_home_path
else
@question_no = current_student.challenge.last_attempted_question
@question = current_student.questions.find_by_question_idx(@question_no)
@displayquestion=current_student.get_displayquestion(@question.question_no)
@timeleft=current_student.challenge.time_limit-Time.now
respond_to do |format|
format.js
format.html # index.html.erb
format.json { render :json => @question }
end
end
end
# PUT /questions/1
# PUT /questions/1.json
def update
@question = current_student.questions.find_by_question_idx(current_student.challenge.last_attempted_question)
@displayquestion=current_student.get_displayquestion(@question.question_no)
@challenge=current_student.challenge
@timeleft=current_student.challenge.time_limit-Time.now
#Check if the answer contains comma by testing against regular expression
#Execute capture array and concaternate only if answer contains a comma
if @displayquestion.answer.include?(",")
if params[:answer].blank?
@question.answer=""
else
@question.answer=params[:answer].join(",")
end
end
respond_to do |format|
if @question.update_attributes(params[:question])
#Marking Routine
current_student.mark_routine
#Update Last Attempted Question only when not exceed
if !@challenge.completed?
@challenge.increment!(:last_attempted_question)
#if complete all questions set complete boolean flag to true or times up
@challenge.check_if_end
end
format.js
format.html { redirect_to @question, :notice => 'Question was successfully saved.' }
format.json { head :ok }
else
format.js { render :text => "Error Saving Question"}
format.html { render :action => "edit" }
format.json { render :json => @question.errors, :status => :unprocessable_entity }
end
end
end
endupdate.js.erb
$('#container').html("<%= escape_javascript(render(:partial => 'questiondisplay', :locals => {:question => @question,:timeleft=>@timeleft})) %>");_questiondisplay.html.erb
<%= content_tag :div, :id=> "timeleft", :data=> {:timeleft=> @timeleft} do -%><% end %>
<fieldset><legend><b><%=t("Question")%>: <%= @question.question_idx %></b></legend>
<div id="content">
<div class="grid_5 left">
<div class="panel01">
<%= form_for(@question,:remote=>true,:html=>{:name=>"question"}) do |f|%>
<p><div class ="row">
<%= label_tag(:topic,@displayquestion.question_text,:class=>"form-label")%>
</div></p>
<% if (@displayquestion.answer.include?(",")) %>
<p>
<%= check_box_tag 'answer[]',"1"%>
<%= label_tag(:choice1,@displayquestion.choice1,:class=>"form-label")%>
</p>
<p>
<%= check_box_tag 'answer[]',"2"%>
<%= label_tag(:choice2,@displayquestion.choice2,:class=>"form-label")%>
</p>
<p>
<%= check_box_tag 'answer[]',"3"%>
<%= label_tag(:choice3,@displayquestion.choice3,:class=>"form-label")%>
</p>
<p>
<%= check_box_tag 'answer[]',"4"%>
<%= label_tag(:choice4,@displayquestion.choice4,:class=>"form-label")%>
</p>
<% else %>
<p>
<%= f.radio_button(:answer, "1") %>
<%= label_tag(:choice1,@displayquestion.choice1,:class=>"form-label")%>
</p>
<p>
<%= f.radio_button(:answer, "2") %>
<%= label_tag(:choice2,@displayquestion.choice2,:class=>"form-label")%>
</p>
<% if !(@displayquestion.choice3.blank?) %>
<p>
<%= f.radio_button(:answer, "3") %>
<%= label_tag(:choice3,@displayquestion.choice3,:class=>"form-label")%>
</p>
<% end %>
<% if !(@displayquestion.choice4.blank?) %>
<p>
<%= f.radio_button(:answer, "4") %>
<%= label_tag(:choice4,@displayquestion.choice4,:class=>"form-label")%>
</p>
<% end %>
<% end %>
<% if @question_no==current_student.challenge.last_question %>
<%= f.submit(:value=>t('finish'),:class =>"large blue awesome",:disable_with=>t('loading')) %>
<% else %>
<%= f.submit(:value=>t('next'),:class =>"large blue awesome",:disable_with=>t('loading')) %>
<% end %>
<% end %>
</div><!-- /.panel01 -->
</div><!-- /.grid-5 -->
<div class="grid_5 right">
<div class="panel02">
<span class="clock">
<span class="timer"><%=t("timer")%></span>
<span id="clock"> </span>
</span>
</div>
</div>
</div></fieldset>index.html.erb
<div id="container">
<%= render :partial=> 'questiondisplay' %>
</div>