skill-assessment-app/app/controllers/admin/question_controller.rb

53 lines
1.3 KiB
Ruby
Raw Normal View History

2016-08-17 17:49:09 -05:00
module Admin
class QuestionController < AdminController
def index
2016-08-19 16:02:18 -05:00
@questions = Question.includes(:quiz).order("quizzes.name", { active: :desc }, :sort)
2016-08-17 17:49:09 -05:00
end
def new
2016-08-19 16:02:18 -05:00
@question = Question.new(active: true)
@quizzes = Quiz.all
2016-08-17 17:49:09 -05:00
end
def create
2016-08-19 16:02:18 -05:00
@quizzes = Quiz.all
@question = Question.create(question_params)
if @question.persisted?
redirect_to admin_questions_path, flash: { notice: "Sucessfully created question" }
else
flash[:error] = "Failed to save question."
render :new
end
2016-08-17 17:49:09 -05:00
end
def view
2016-08-19 16:02:18 -05:00
@question = Question.includes(:quiz).find(params[:question_id])
2016-08-17 17:49:09 -05:00
end
def edit
2016-08-19 16:02:18 -05:00
@quizzes = Quiz.all
@question = Question.includes(:quiz).find(params[:question_id])
2016-08-17 17:49:09 -05:00
end
def update
2016-08-19 16:02:18 -05:00
@quizzes = Quiz.all
@question = Question.find(params[:question_id])
if @question.update_attributes(question_params)
redirect_to admin_question_path(@question.to_i),
flash: { notice: "Sucessfully updated question" }
else
flash[:error] = "Failed to update question."
render :edit
end
2016-08-17 17:49:09 -05:00
end
2016-08-19 16:02:18 -05:00
private
def question_params
params.require(:question).permit(:quiz_id, :question, :category, :input_type, :input_options, :sort)
end
2016-08-17 17:49:09 -05:00
end
end