Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 获取404找不到默认错误页“;活动记录::RecordNotFound";当我有一个自定义页面的时候_Ruby On Rails_Http Status Code 404_Custom Error Pages - Fatal编程技术网

Ruby on rails 获取404找不到默认错误页“;活动记录::RecordNotFound";当我有一个自定义页面的时候

Ruby on rails 获取404找不到默认错误页“;活动记录::RecordNotFound";当我有一个自定义页面的时候,ruby-on-rails,http-status-code-404,custom-error-pages,Ruby On Rails,Http Status Code 404,Custom Error Pages,我有一个404&500状态代码的自定义错误页面,当我放置localhost:3000/something.html时,它可以正常工作。但当我将localhost:3000/controller/element\u放在一个模型中时,它就不起作用了 routes.rb: if Rails.env.production? then unless Rails.application.config.consider_all_requests_local get '*not_found', to

我有一个404&500状态代码的自定义错误页面,当我放置localhost:3000/something.html时,它可以正常工作。但当我将localhost:3000/controller/element\u放在一个模型中时,它就不起作用了

routes.rb:

if Rails.env.production? then
  unless Rails.application.config.consider_all_requests_local
    get '*not_found', to: 'errors#error_404'
    get '*internal_server_error', to: 'errors#error_500'
  end
else
  unless
    get '*not_found', to: 'errors#error_404'
    get '*internal_server_error', to: 'errors#error_500'
  end
end
错误控制器:

def error_404
    render_error 404
end

def error_500
    render_error 500
end

private
   def render_error(status)
       respond_to do |format|
           format.html { render 'error_' + status.to_s() + '.html', :status => status, :layout => 'errors'}
           format.all { render :nothing => true, :status => status }
   end
end
rescue_from ActiveRecord::RecordNotFound do |exception|
  render_error 404
end


def render_error(status)
  respond_to do |format|
    format.html { render 'error_' + status.to_s() + '.html', :status => status, :layout => 'errors'}
    format.all { render :nothing => true, :status => status }
  end
end

您必须将其放入应用程序控制器:

def error_404
    render_error 404
end

def error_500
    render_error 500
end

private
   def render_error(status)
       respond_to do |format|
           format.html { render 'error_' + status.to_s() + '.html', :status => status, :layout => 'errors'}
           format.all { render :nothing => true, :status => status }
   end
end
rescue_from ActiveRecord::RecordNotFound do |exception|
  render_error 404
end


def render_error(status)
  respond_to do |format|
    format.html { render 'error_' + status.to_s() + '.html', :status => status, :layout => 'errors'}
    format.all { render :nothing => true, :status => status }
  end
end
实际上,您的ErrorController将由路由触发,但您必须为异常添加逻辑