Skip to content

Instantly share code, notes, and snippets.

@EminenceHC
Last active December 27, 2015 14:39
Show Gist options
  • Select an option

  • Save EminenceHC/7341822 to your computer and use it in GitHub Desktop.

Select an option

Save EminenceHC/7341822 to your computer and use it in GitHub Desktop.
Serialized Hash in Strong Parameters
class ResultSet < ActiveRecord::Base
belongs_to :survey
belongs_to :student
accepts_nested_attributes_for :survey
attr_accessor :score, :correct_count, :total_count, :percent
serialize :serial
end
class ResultSetsController < ApplicationController
before_filter :new
def index
@quiz_id = ResultSet.all.map { |u| [u.survey.name, u.survey_id]}.uniq
@quiz_student_name = ResultSet.all.map { |u| [u.student.user.name, u.student_id]}.uniq
if current.loginable_type == 'Student'
@student_only = params[:student_id] = current_user.loginable.id
end
runquery = ResultSet
runquery = runquery.where(:survey_id => params[:id]) if params[:id].present?
runquery = runquery.where(:student_id => params[:student_id]) if params[:student_id].present?
runquery = runquery.select('result_sets.*')
@query = runquery
@query.each do |a|
@arr = []
@actual = []
@correct = []
@score = []
a.serial.to_a.each do |key,value|
@actual << value
end
a.survey.questions.each do |b|
@id = b.answers
@id.each do |c|
if c.correct == true
@correct = @arr << c.id
end
end
end
# Virtual Attributes for View
@score = @correct.collect{|x| "#{x}"} & @actual
a.correct_count = @score.count.to_f
a.total_count = @actual.count.to_f
a.percent = (a.correct_count / a.total_count) * 100
end
end
def new
@result_set = ResultSet.new
end
def create
@result_set = ResultSet.new(result_set_params)
@result_set.save!
if @result_set.save
redirect_to result_sets_path, notice: "Quiz Completed Successfully"
else
redirect_to result_sets_path, notice: "There was a problem, Quiz not saved."
end
end
def show
end
private
def result_set_params
params.require(:result_set).permit(:id, :student_id, :survey_id, :created_at, :updated_at, :serial => {}, survey_attributes: [:id, :name, :created_at, :updated_at, :student_id])
end
end
<h1><%= @survey.name %></h1>
<%= simple_form_for @result_set, url: result_sets_path do |f| %>
<ol class="questions">
<% @survey.questions.each do |question| %>
<li>
<%= question.content %>
<ol class="answers">
<% question.answers.each do |answer| %>
<li><%= answer.content %> <input type="radio" name="result_set[serial][<%= question.id %>]" value="<%= answer.id %>" style="margin-top:-2px;" /></li>
<% end %>
</ol>
</li>
<% end %>
</ol>
<%= f.input :student_id, :as => "hidden", :input_html => { :value => current_user.loginable.id } %>
<%= f.input :survey_id, :as => "hidden", :input_html => { :value => @survey.id } %>
<%= f.submit 'Done', :class => 'btn btn-primary' %>
<% end %>
<% if current.loginable_type != 'Student' %>
<%= link_to 'Edit', edit_survey_path(@survey) %> |
<% end %>
<%= link_to 'Back to Surveys', surveys_path %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment