Skip to content

Commit

Permalink
Remove XML and using first or initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
rposborne committed May 23, 2014
1 parent e8ecd1c commit 6d92960
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions app/controllers/responses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
class ResponsesController < ApplicationController
require 'csv'
# GET /responses
# GET /responses.xml
layout proc { |c| c.request.xhr? ? false : 'application' }
def index
@responses = Response.all

respond_to do |format|
format.html # index.html.erb
format.xml { render xml: @responses }
end
end

# GET /responses/1
# GET /responses/1.xml
def show
@response = Response.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render xml: @response }
end
end

# GET /responses/new
# GET /responses/new.xml
def new
@response = Response.new

respond_to do |format|
format.html # new.html.erb
format.xml { render xml: @response }
end
end

Expand All @@ -53,67 +47,82 @@ def export_to_csv

csv_string = CSV.generate do |csv|
# header row
csv << ['id', 'participant_id', 'group', 'error', 'essay', 'Correct?', 'Field Before Correction', 'Seconds to Complete', 'Round','Treatment', 'Time Corrected']
csv << [
'id',
'participant_id',
'group',
'error',
'essay',
'Correct?',
'Field Before Correction',
'Seconds to Complete',
'Round',
'Treatment',
'Time Corrected'
]

# data rows
@responses.each do |response|
csv << [response.id, response.user_id, response.user.group, response.error, response.essay, response.correct, response.uncorrected, response.round_number, response.user.time_to_complete, response.controller, response.created_at]
csv << [
response.id,
response.user_id,
response.user.group,
response.error,
response.essay,
response.correct,
response.uncorrected,
response.round_number,
response.user.time_to_complete,
response.controller,
response.created_at
]
end
end

# send it to the browsah
send_data csv_string,
type: 'text/csv; charset=iso-8859-1; header=present',
disposition: 'attachment; filename=responses.csv'
type: 'text/csv; charset=iso-8859-1; header=present',
disposition: 'attachment; filename=responses.csv'
end

# POST /responses
# POST /responses.xml
def create
@user = User.find_or_create_by_id(params[:participant_id], group: params[:group])
@response = @user.responses.where(:error => params[:response][:id], :round_number => params[:response][:round_number]).first_or_create
@response.update_attributes(params[:response])
@response = @user.responses.where(error: params[:response][:id], round_number: params[:response][:round_number]).first_or_initalize(params[:response])

respond_to do |format|
if @response.save
format.js
format.html { redirect_to(@response, notice: 'Response was successfully created.') }
format.xml { render xml: @response, status: :created, location: @response }

else
format.js { render js: "#{ @response.errors}" }
format.html { render action: 'new' }
format.xml { render xml: @response.errors, status: :unprocessable_entity }

end
end
end

# PUT /responses/1
# PUT /responses/1.xml
def update
@response = Response.find(params[:id])

respond_to do |format|
if @response.update_attributes(params[:response])
format.html { redirect_to(@response, notice: 'Response was successfully updated.') }
format.xml { head :ok }
else
format.html { render action: 'edit' }
format.xml { render xml: @response.errors, status: :unprocessable_entity }
end
end
end

# DELETE /responses/1
# DELETE /responses/1.xml
def destroy
@response = Response.find(params[:id])
@response.destroy

respond_to do |format|
format.html { redirect_to(responses_url) }
format.xml { head :ok }
end
end
end

0 comments on commit 6d92960

Please sign in to comment.