AutoCommit Don Jul 30 01:03:05 CEST 2015

This commit is contained in:
Andreas Stephanides
2015-07-30 01:03:05 +02:00
parent 97df081b33
commit f161d15174
7 changed files with 31 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,4 @@
<div style="background:red"><b><%= answeredquestion.title%></b>
<% answeredquestion.choices.each do |c| %>
<%= c.to_s %><%= answeredquestion.answers.where(choice_id: c.id).count %>
<% end %>

View File

@@ -1,2 +1,8 @@
<b><%= question.title%></b>
<%= form_for %>
<div style="background:red"><b><%= question.title%></b>
<%= semantic_form_for question, url: answer_survey_question_path do |f| %>
<%= f.input :selected, as: :check_boxes, collection: question.choices %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
</div>

View File

@@ -16,6 +16,7 @@
</p>
<%= render @survey_question %>
<%= render partial: "answeredquestion", object: @survey_question %>
<%= link_to 'Edit', edit_survey_question_path(@survey_question) %> |
<%= link_to 'Back', survey_questions_path %>

View File

@@ -13,7 +13,7 @@
namespace :survey do
resources :questions do
member do
:answer
put :answer
end
end
end