diff --git a/app/controllers/survey/questions_controller.rb b/app/controllers/survey/questions_controller.rb index e3bfc43..e5ecbaf 100644 --- a/app/controllers/survey/questions_controller.rb +++ b/app/controllers/survey/questions_controller.rb @@ -10,6 +10,10 @@ class Survey::QuestionsController < ApplicationController end end def answer + @survey_question = Survey::Question.find(params[:id]) + + + @survey_question.do_answer(params[:survey_question][:selected],current_user) render :show end # GET /survey/questions/1 diff --git a/app/models/survey/choice.rb b/app/models/survey/choice.rb index 921e636..727cd0a 100644 --- a/app/models/survey/choice.rb +++ b/app/models/survey/choice.rb @@ -2,4 +2,7 @@ class Survey::Choice < ActiveRecord::Base belongs_to :question, class_name: 'Survey::Question' attr_accessible :picture, :sort, :text has_many :answers, class_name: 'Survey::Answer' + def to_s + self.text + end end diff --git a/app/models/survey/question.rb b/app/models/survey/question.rb index 7815d40..fea707e 100644 --- a/app/models/survey/question.rb +++ b/app/models/survey/question.rb @@ -10,7 +10,16 @@ class Survey::Question < ActiveRecord::Base c=Survey::Choice.new(title: "Nein") c.save this.choices << c - + end + + def do_answer(choice_ids, user) + self.answers.where(user_id: user.id).each {|a| a.delete} + choice_ids.each do |c| + if self.choice_ids.include?(c) + a=Survey::Answer.new(user_id: user.id, choice_id: c) + a.save + end + end end end diff --git a/app/views/survey/questions/_answeredquestion.html.erb b/app/views/survey/questions/_answeredquestion.html.erb new file mode 100644 index 0000000..4583486 --- /dev/null +++ b/app/views/survey/questions/_answeredquestion.html.erb @@ -0,0 +1,4 @@ +