Ruby on rails 获取404找不到默认错误页“;活动记录::RecordNotFound";当我有一个自定义页面的时候
我有一个404&500状态代码的自定义错误页面,当我放置localhost:3000/something.html时,它可以正常工作。但当我将localhost:3000/controller/element\u放在一个模型中时,它就不起作用了 routes.rb: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
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将由路由触发,但您必须为异常添加逻辑