在json响应中排除Rails Heroku 500错误

时间:2014-09-21 13:19:11

标签: ruby-on-rails heroku jbuilder

在我的heroku app中查询survey.json或survey /: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

编辑添加我的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"}

我也尝试清除浏览器缓存,但似乎也没有解决问题。

1 个答案:

答案 0 :(得分:1)

似乎jbuilder抛出500错误。如果您的Ruby代码中存在错误,您将在日志中获得堆栈跟踪。

看看this。希望它有所帮助。