Created
March 31, 2014 15:15
-
-
Save jsuwo/9894686 to your computer and use it in GitHub Desktop.
Skeleton for a quiz app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for [@test, @attempt, @question, @response] do |f| %> | |
<label><%= @question.question_text %></label> | |
<%= f.text_area :response_text %> | |
<%= f.submit 'Next >' %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuestionsController < ApplicationController | |
def next | |
@test = Test.find(params[:test_id]) | |
@attempt = Attempt.find(params[:attempt_id]) | |
@question = @test.questions.find(params[:id]) | |
if @question.last? | |
redirect_to tests_path | |
else | |
redirect_to new_test_attempt_question_response_path(@test, @attempt, @question.lower_item) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResponsesController < ApplicationController | |
#load_and_authorize_resource :test | |
#load_and_authorize_resource :attempt, through: :test | |
#load_and_authorize_resource :question, through: :test | |
#load_and_authorize_resource :response, through: :attempt | |
before_filter :load_objects | |
def new | |
@response = @question.responses.build | |
end | |
def create | |
# Rails 3: params[:response] | |
@response = @attempt.responses.build(params.require(:response).permit(:response_text)) | |
@response.question = @question | |
if @response.save | |
redirect_to next_test_attempt_question_path(@test, @attempt, @question) | |
else | |
render 'new' | |
end | |
end | |
def edit | |
@response = @attempt.responses.find(params[:id]) | |
end | |
def update | |
@response = @attempt.responses.find(params[:id]) | |
@response.question = @question | |
if @response.update_attributes(params.require(:response).permit(:response_text)) | |
redirect_to next_test_attempt_question_path(@test, @attempt, @question) | |
else | |
render 'new' | |
end | |
end | |
private | |
def load_objects | |
@test = Test.find(params[:test_id]) | |
@attempt = Attempt.find(params[:attempt_id]) | |
@question = @test.questions.find(params[:question_id]) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :tests do | |
post :start, on: :member | |
resources :attempts do | |
resources :questions do | |
get :next, on: :member | |
resources :responses | |
end | |
end | |
end | |
root 'tests#index' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestsController < ApplicationController | |
def index | |
@tests = Test.all | |
end | |
def start | |
@test = Test.find(params[:id]) | |
@attempt = @test.attempts.create | |
redirect_to new_test_attempt_question_response_path(@test, @attempt, @test.questions.first) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment