I'm struggling to figure out why I'm getting a status of 500 when querying for surveys.json or surveys/:id.json in my heroku app. Any idea what I should check or what might be the problem?
My first intuition was that maybe it needed a heroku run rake db:migrate
but that didn't seem to fix the issue. I also tried doing a heroku run rake assets:clean/compile
but neither of those helped either.
When running the app in development or RAILS_ENV=production
it works as expected and the surveys json returns with no issues.
I was playing around with my surveys jbuilder however so I suspect that it has something to do with that, but it's weird that it works fine in dev/running locally in production.
Also worth nothing - I'm running PG in development/test/production so I don't think it's a difference in database with Heroku.
Here's my jbuilder file:
json.array!(@surveys) do |survey|
json.extract! survey, :id, :title, :survey_limit, :status, :number_taken, :created_at, :updated_at, :created_date, :user_id
json.questions survey.questions do |json, question|
json.extract! question, :id, :title, :single_response, :randomize, :terminate, :free_text, :number_only, :min_number, :max_number, :demographic, :demographic_item, :question_number
json.answers question.answers do |json, answer|
json.title answer.title
json.id answer.id
json.next_question_id answer.next_question_id
json.free_text answer.free_text
json.branching answer.branching
end
end
json.url survey_url(survey, format: :json)
json.responses survey.responses do |json, response|
json.extract! response, :id, :survey_id, :completed, :appuser_id, :created_at, :updated_at
appuser = response.appuser
json.extract! appuser, :state_code
json.extract! appuser, :age
json.extract! appuser, :gender
end
end
The change I made yesterday was adding:
json.url survey_url(survey, format: :json)
json.responses survey.responses do |json, response|
json.extract! response, :id, :survey_id, :completed, :appuser_id, :created_at, :updated_at
appuser = response.appuser
json.extract! appuser, :state_code
json.extract! appuser, :age
json.extract! appuser, :gender
end
However, I tried removing that piece and it still returned a status of 500 so I'm kind of doubtful that's actually what's going on.
For kicks I'm adding my model/controller.
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :fetch, :edit, :update, :destroy]
# GET /surveys
# GET /surveys.json
def index
if !cookies[:appuser_token]
appuser = Appuser.create
sign_in_appuser appuser
else
appuser = current_appuser
end
@surveys = Survey.where(user_id: cookies[:user_id])
end
# GET /surveys/1
# GET /surveys/1.json
def show
if !cookies[:appuser_token]
appuser = Appuser.create
sign_in_appuser appuser
else
appuser = current_appuser
end
respond_to do |format|
format.json
format.csv { send_data @survey.to_csv }
format.xlsx
end
end
def fetch
respond_to do |format|
format.json
end
end
# GET /surveys/new
def new
unless signed_in?
redirect_to signin_path
end
@survey = Survey.new
end
# GET /surveys/1/edit
def edit
end
# POST /surveys
# POST /surveys.json
def create
unless signed_in?
redirect_to signin_path
end
@survey = Survey.new(survey_params)
respond_to do |format|
if @survey.save
if @survey.status == "Submitted"
SurveyMailer.survey_created(@survey).deliver
end
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render action: 'show', status: :created, location: @survey }
else
format.html { render action: 'new' }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /surveys/1
# PATCH/PUT /surveys/1.json
def update
unless signed_in?
redirect_to signin_path
end
respond_to do |format|
if @survey.update(survey_params)
if @survey.status == "Submitted"
SurveyMailer.survey_created(@survey).deliver
end
format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
# DELETE /surveys/1
# DELETE /surveys/1.json
def destroy
unless signed_in?
redirect_to signin_path
end
@survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_survey
@survey = Survey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
params.require(:survey).permit(:number_taken, :survey_limit, :title, :status, :user_id)
end
end
Model:
class Survey < ActiveRecord::Base
has_many :questions
has_many :responses
belongs_to :user
validates_presence_of :title
def created_date
"#{created_at}".to_date
end
def complete_survey
num = number_taken + 1
update_attributes(number_taken: num)
if (number_taken >= survey_limit)
update_attributes(status: "Completed")
end
end
def to_csv(options = {})
question_titles = questions.map { |r| r.title }
CSV.generate(options) do |csv|
csv << question_titles
responses.each do |response|
csv_row = []
choice_array = []
questions.each do |question|
csv_cell = ""
choices = Choice.where(question_id: question.id, response_id: response.id)
if !question.single_response
choices.each do |choice|
csv_cell = csv_cell + "|" unless csv_cell == ""
answer = Answer.find(choice.answer_id)
if answer.free_text
title = choice.free_text_response
csv_cell = csv_cell + title
else
index = question.answers.find_index answer
title = Answer.find(choice.answer_id).title
csv_cell = csv_cell + index.to_s + "|" + title
end
end
else
choices.each do |choice|
csv_cell = csv_cell + "|" unless csv_cell == ""
answer = Answer.find(choice.answer_id)
if answer.free_text
title = choice.free_text_response
csv_cell = csv_cell + title
else
index = question.answers.find_index answer
title = Answer.find(choice.answer_id).title
csv_cell = index.to_s + "|" + title
end
end
end
csv_row << csv_cell
end
csv << csv_row
end
end
end
end
EDIT Adding my Heroku Log (the last line is an example of the status 500)
←[36m2014-09-21T14:38:42.114954+00:00 heroku[router]:←[0m at=info method=GET pat
h="/surveys" host=example.herokuapp.com request_id=63cb25e9-a153-4028-9233-
707f609ae9e4 fwd="71.217.213.199" dyno=web.1 connect=118ms service=1282ms status
=500 bytes=375
←[36m2014-09-21T14:40:46.090422+00:00 heroku[router]:←[0m at=info method=HEAD pa
th="/" host=example.herokuapp.com request_id=1cb4f5fc-24f4-4314-bdcd-1867f7
0e4e46 fwd="74.86.158.106" dyno=web.1 connect=279ms service=439ms status=200 byt
es=936
←[36m2014-09-21T14:43:30.614544+00:00 heroku[router]:←[0m at=info method=GET pat
h="/" host=example.herokuapp.com request_id=c47a6941-5fa1-47de-b036-995fa0c
5b70c fwd="54.166.22.65" dyno=web.1 connect=3ms service=7ms status=301 bytes=229
←[36m2014-09-21T14:45:45.404793+00:00 heroku[router]:←[0m at=info method=HEAD pa
th="/" host=example.herokuapp.com request_id=6fcdc0b6-05e2-4193-aaef-ff3b3e
3c03f3 fwd="74.86.158.106" dyno=web.1 connect=3ms service=15ms status=200 bytes=
936
←[36m2014-09-21T14:47:22.149479+00:00 heroku[router]:←[0m at=info method=GET pat
h="/surveys" host=example.herokuapp.com request_id=0a53ed27-8ac8-47bd-9a85-
5028832f6662 fwd="71.217.213.199" dyno=web.1 connect=2ms service=33ms status=200
bytes=1684
←[36m2014-09-21T14:47:22.462054+00:00 heroku[router]:←[0m at=info method=GET pat
h="/assets/application-c2d8e61338c9783113ef91c7e33789ae.js" host=example.he
rokuapp.com request_id=fbeeaa36-c871-483e-9071-87ba87fccc75 fwd="71.217.213.199"
dyno=web.1 connect=1ms service=5ms status=304 bytes=276
←[36m2014-09-21T14:47:22.454225+00:00 heroku[router]:←[0m at=info method=GET pat
h="/assets/application-795c0ab8f1ce18d00247c018b9b1fe37.css" host=example.h
erokuapp.com request_id=c593279b-879b-44c6-8f46-46bba176bbee fwd="71.217.213.199
" dyno=web.1 connect=3ms service=5ms status=304 bytes=276
←[36m2014-09-21T14:47:22.780301+00:00 heroku[router]:←[0m at=info method=GET pat
h="/assets/fontawesome-webfont-b83782d932b98da1712aaebe1028fa9d.woff?v=4.2.0" ho
st=example.herokuapp.com request_id=668d68c7-0430-419f-8961-9d00327dd4a1 fw
d="71.217.213.199" dyno=web.1 connect=0ms service=43ms status=304 bytes=276
←[36m2014-09-21T14:47:22.776478+00:00 heroku[router]:←[0m at=info method=GET pat
h="/surveys" host=example.herokuapp.com request_id=16060ffb-7f35-4b25-ae64-
9c20224f9a3c fwd="71.217.213.199" dyno=web.1 connect=1ms service=51ms status=500
bytes=375
JSON Response:
{"status":"500","error":"Internal Server Error"}
I also tried clearing the browser cache but that didn't seem to fix the issue either.
Seems that jbuilder is throwing that 500 error. If there was an error in you Ruby code, you would have gotten stacktrace in the log.
Take a look at this. Hope it helps.