这是我目前的处理错误:
#routes.rb
match "/404", :to => "errors#not_found", :via => :all, as: 'not_found'
match "/500", :to => "errors#internal_server_error", :via => :all
#ErrorsController.rb
class ErrorsController < ApplicationController
def not_found
render(:status => 404)
end
def internal_server_error
render(:status => 500)
end
end
#views/errors/not_found.html.erb
(static HTML page)
我的问题是当我在网站上输入错误的网址时,例如www.example.com/asdfasdf,我会被重定向到www.example.com/404。相比之下,如果我访问www.stackoverflow.com/asdfasdf,我会收到“找不到页面”错误,但网址仍然是www.stackoverflow.com/asdfasdf。
我想改变行为,以便它在Stack Overflow中与它的工作方式相匹配,在那里我显示404页面,但URL保持与我输入的相同。最好的方法是什么?谢谢!
答案 0 :(得分:2)
试试这个:
# routes.rb
match "/404", :to => "errors#not_found", :via => :all, as: 'not_found'
match "/500", :to => "errors#internal_server_error", :via => :all
# errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render :file => 'public/404.html', :status => :not_found, :layout => false
end
def internal_server_error
render :file => 'public/500.html', :status => :not_found, :layout => false
end
end