ActiveRecord 500错误,但修复后修复

时间:2018-01-15 23:21:00

标签: ruby-on-rails activerecord

当我在生产模式下向ActiveRecord模型添加新项目时,我的rails网站会抛出500错误。在开发模式下,它会抛出404.在生产和开发中,简单刷新页面可以解决问题。

这是日志。

2018-01-15T23:11:26.565002+00:00 app[web.1]: I, [2018-01-15T23:11:26.564925 #4]  INFO -- : [eb00b0fb-d0b8-4677-b952-9704b19c9f2d] Completed 500 Internal Server Error in 12ms (ActiveRecord: 5.6ms)
2018-01-15T23:11:26.563230+00:00 app[web.1]: D, [2018-01-15T23:11:26.563150 #4] DEBUG -- : [eb00b0fb-d0b8-4677-b952-9704b19c9f2d]    (4.3ms)  COMMIT
2018-01-15T23:11:26.565969+00:00 app[web.1]: F, [2018-01-15T23:11:26.565895 #4] FATAL -- : [eb00b0fb-d0b8-4677-b952-9704b19c9f2d]   
2018-01-15T23:11:26.566089+00:00 app[web.1]: F, [2018-01-15T23:11:26.565970 #4] FATAL -- : [eb00b0fb-d0b8-4677-b952-9704b19c9f2d] ActionView::MissingTemplate (Missing template /index with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
2018-01-15T23:11:26.566091+00:00 app[web.1]:   * "/app/app/views"
2018-01-15T23:11:26.566092+00:00 app[web.1]:   * "/app/vendor/bundle/ruby/2.3.0/gems/ckeditor-4.2.4/app/views"
2018-01-15T23:11:26.566094+00:00 app[web.1]: ):
2018-01-15T23:11:26.566291+00:00 app[web.1]: F, [2018-01-15T23:11:26.566237 #4] FATAL -- : [eb00b0fb-d0b8-4677-b952-9704b19c9f2d] app/controllers/questions_controller.rb:31:in `create'
2018-01-15T23:11:26.566163+00:00 app[web.1]: F, [2018-01-15T23:11:26.566079 #4] FATAL -- : [eb00b0fb-d0b8-4677-b952-9704b19c9f2d]   
2018-01-15T23:11:26.567079+00:00 heroku[router]: at=info method=POST path="/questions" host=stclprores.herokuapp.com request_id=eb00b0fb-d0b8-4677-b952-9704b19c9f2d fwd="96.88.61.218" dyno=web.1 connect=1ms service=21ms status=500 bytes=1827 protocol=http

我对正在发生的事情的最好猜测是模型花了很长时间才更新,并且页面试图在尝试保存模型的同时从模型中提取信息,因此它返回Nil值并且页面崩溃。但是,我可能完全错了。谁能指出我在这里的正确轨道。 Rails试图做什么,以及我如何阻碍它?

这是我的控制器:

def edit
    @question = Question.find(params[:id])
end 

def update
    @question = Question.find(params[:id])
    if @question.save 
        redirect_to '/'
    else
        redirect_to '/error/could_not_save'
    end 
end 

def index 
    @questions = Question.all 
    respond_to do |format|
        format.js
        format.html
    end 
end 

def new 
    @question = Question.new 
end 

def create 
    @question = Question.new(question_params)
    if @question.save
        redirect_to '/' 
    else 
        redirect_to '/error/could_not_save'
    end 
end 

def destroy
    Question.find(params[:id]).destroy
    redirect_to '/index'
end 

private
    def question_params
        params.require(:question).permit(:topic, :prompt, :correct_answer, :wrong_answer_one, :wrong_answer_two, :wrong_answer_three)
    end

这是config / routes:

Rails.application.routes.draw do

  resources :questions

  root 'static#home'

  get '/home' => 'static#home'
  get '/error/unknown_error' => 'static#unkown_error'
  get '/error/could_not_save' => 'static#could_not_save'

  get '/:id/edit' => 'questions#edit', as: @question
  post '/index' => 'questions#update'
  get '/home' => 'questions#home'
  get '/new' => 'questions#new'
  get '/questions' => 'questions#index'
  get '/question' => 'question#index'
  post '/index/' => 'questions#create'
  get '/index' => 'questions#index'
  get '/practice' => 'questions#practice'
end

0 个答案:

没有答案