Sha256: bca70be9d9f80651248125d6bccdde42d0ec0c6b835702bf5855ca02ada13ed6

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module Rapidfire
  class QuestionsController < Rapidfire::ApplicationController
    before_action :authenticate_administrator!
    before_action :find_survey!
    before_action :find_question!, :only => [:edit, :update, :destroy]

    def index
      @questions = @survey.questions
    end

    def new
      @question_form = QuestionForm.new(:survey => @survey)
    end

    def create
      form_params = question_params.merge(:survey => @survey)

      save_and_redirect(form_params, :new)
    end

    def edit
      @question_form = QuestionForm.new(:question => @question)
    end

    def update
      form_params = question_params.merge(:question => @question)
      save_and_redirect(form_params, :edit)
    end

    def destroy
      @question.destroy
      respond_to do |format|
        format.html { redirect_to index_location }
        format.js
      end
    end

    private

    def save_and_redirect(params, on_error_key)
      @question_form = QuestionForm.new(params)
      @question_form.save

      if @question_form.errors.empty?
        respond_to do |format|
          format.html { redirect_to index_location }
          format.js
        end
      else
        respond_to do |format|
          format.html { render on_error_key.to_sym }
          format.js
        end
      end
    end

    def find_survey!
      @survey = Survey.find(params[:survey_id])
    end

    def find_question!
      @question = @survey.questions.find(params[:id])
    end

    def index_location
      rapidfire.survey_questions_url(@survey)
    end

    def question_params
      params.require(:question).permit!
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rapidfire-4.0.0 app/controllers/rapidfire/questions_controller.rb