【发布时间】:2014-11-15 13:34:04
【问题描述】:
在我的 heroku 应用程序中查询surveys.json 或surveys/:id.json 时,我很难弄清楚为什么我的状态为500。知道我应该检查什么或可能是什么问题吗?
我的第一个直觉是它可能需要heroku run rake db:migrate,但这似乎并不能解决问题。我也尝试过heroku run rake assets:clean/compile,但这些都没有帮助。
在开发或RAILS_ENV=production 中运行应用程序时,它会按预期工作,并且调查 json 会毫无问题地返回。
我正在玩我的调查 jbuilder 但是,所以我怀疑它与此有关,但奇怪的是它在开发/本地生产中运行良好。
也一文不值 - 我在开发/测试/生产中运行 PG,所以我认为这与 Heroku 的数据库没有区别。
这是我的 jbuilder 文件:
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
我昨天所做的更改是添加:
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
但是,我尝试删除该部分,但它仍然返回 500 的状态,所以我有点怀疑这实际上是怎么回事。
为了好玩,我正在添加我的模型/控制器。
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
型号:
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 添加我的 Heroku 日志(最后一行是状态 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 响应:
{"status":"500","error":"Internal Server Error"}
我也尝试清除浏览器缓存,但似乎也没有解决问题。
【问题讨论】:
-
你检查过heroku日志吗?
-
是的,它几乎只是说 GETsurveys.json 和 500 的状态
-
你能把日志中的相关行贴出来吗?
-
是的,我发帖的时候你问得对:)
-
jbuilder 抛出 500 错误。看看this。如果你的 Ruby 代码有错误,你会在日志中得到堆栈跟踪。
标签: ruby-on-rails heroku jbuilder