Last active
August 25, 2017 17:08
-
-
Save staycreativedesign/cd37cfd2071fbc2dc86875037e067c79 to your computer and use it in GitHub Desktop.
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 CreateAnswers | |
def initialize(params, user) | |
@question_params = params | |
@user = user | |
@questionnaire_id = params["questionnaire_id"] | |
@questionnaire_name = params["questionnaire_name"] | |
end | |
def run! | |
create_answers | |
end | |
private | |
def create_answers | |
q = Questionnaire.find(@questionnaire_id) | |
@user.questionnaires << q | |
q.update(question_params) | |
end | |
def question_params | |
@question_params.require(:questionnaire).permit(questions_attributes: [ :id, questionnaire_answer_attributes: [:description] ]) | |
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 Questionnaire < ApplicationRecord | |
has_many :questions | |
has_many :member_questionnaires | |
has_many :users, through: :member_questionnaires | |
accepts_nested_attributes_for :questions | |
end | |
class Question < ApplicationRecord | |
belongs_to :questionnaire | |
has_one :questionnaire_answer | |
accepts_nested_attributes_for :questionnaire_answer | |
end | |
class QuestionnaireAnswer < ApplicationRecord | |
belongs_to :question | |
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
%h1 | |
questionnaire_answers#new | |
= form_for @questionnaire, url: questionnaire_answers_path, method: :post do |f| | |
= hidden_field_tag 'questionnaire_id', @questionnaire.id | |
= hidden_field_tag 'name', @questionnaire.name | |
- @questionnaire.questions.each.with_index do |q, index| | |
%h1 | |
= q.description | |
= f.fields_for :questions, q do |question_fields| | |
= question_fields.fields_for :questionnaire_answer, q.build_questionnaire_answer do |answer_fields| | |
= answer_fields.text_field :description, id: index | |
= f.submit "Save answers" |
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 QuestionnaireAnswersController < ApplicationController | |
def new | |
find_questionnaire | |
@answer = QuestionnaireAnswer.new | |
end | |
def create | |
@answers_creation = CreateAnswers.new(params, current_user) | |
@answers_creation.run! | |
end | |
def edit | |
end | |
def update | |
end | |
private | |
def find_questionnaire | |
@questionnaire = Questionnaire.find(params[:id]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment