module Admin class QuestionController < AdminController def index @questions = Question.includes(:quiz).order("quizzes.name", { active: :desc }, :sort) end def new @question = Question.new(active: true) @quizzes = Quiz.all end def create @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 end def view @question = Question.includes(:quiz).find(params[:question_id]) end def edit @quizzes = Quiz.all @question = Question.includes(:quiz).find(params[:question_id]) end def update @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 end private def question_params params.require(:question).permit(:quiz_id, :question, :category, :input_type, :input_options, :sort) end end end