Skip to content

Instantly share code, notes, and snippets.

@jsuwo
Created March 31, 2014 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsuwo/9894686 to your computer and use it in GitHub Desktop.
Save jsuwo/9894686 to your computer and use it in GitHub Desktop.
Skeleton for a quiz app
<%= form_for [@test, @attempt, @question, @response] do |f| %>
<label><%= @question.question_text %></label>
<%= f.text_area :response_text %>
<%= f.submit 'Next >' %>
<% end %>
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
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
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'
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