如何营救在Rails中找不到404页面? [英] How to rescue page not found 404 in rails?
问题描述
如果用户在rails中添加错误的url,如何找不到救援页面。如果网址无效,我希望显示公共文件夹中的404页。怎么做?我正在浏览它,但找不到解决方案。我尝试了多种方法来解决此问题,但是它们似乎没有用。
我被困在这里,请帮忙。
How to rescue page not found if user add wrong url in rails. I hope to show 404 page present in public folder if the url is invalid. How to do that? I was browsing about it but could not find a solution. I tried many ways to fix the problem, but they don't seem to be working. I am stuck here, please help.
推荐答案
我找到了解决方案,请检查一下=> http://techoctave.com/c7/posts/ 36-rails-3-0-从路由错误中解救(最佳解决方案)
i found the solution, check this out => http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution (great solution)
routes.rb:
routes.rb :
# at the end of you routes.rb
match '*a', :to => 'errors#routing', via: :get
errors_controller.rb:
errors_controller.rb :
class ErrorsController < ApplicationController
def routing
render_404
end
end
application.rb:
application.rb :
rescue_from ActionController::RoutingError, :with => :render_404
private
def render_404(exception = nil)
if exception
logger.info "Rendering 404: #{exception.message}"
end
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
这篇关于如何营救在Rails中找不到404页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!