Tag: http error

如何正确呈现自定义404和500页?

是否有办法告诉Rails呈现您的自定义错误页面(例如,您在ErrorsController编写的错误页面)? 我搜索了很多主题,看起来有点工作的是添加到你的ApplicationController东西 if Rails.env.production? rescue_from Exception, :with => :render_error rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found rescue_from ActionController::UnknownController, :with => :render_not_found rescue_from ActionController::UnknownAction, :with => :render_not_found end 然后你按照你想要的方式编写你的方法render_error和render_not_found 。 在我看来,这似乎是一个非常不优雅的解决方案。 此外,这很糟糕,因为您必须确切知道可能发生的所有错误是什么。 这是一个临时解决方案。 而且,实际上没有简单的方法来拯救ActionController::RoutingError 。 我看到一种方法是添加类似的东西 get “*not_found”, :to => “errors#not_found” 到你的routes.rb 。 但是如果你想手动提升ActionController::RoutingError呢? 例如,如果非管理员的人试图通过猜测URL来“管理”控制器。 在那些情况下,我更喜欢提高404而不是某种类型的“未经授权的访问”错误,因为这实际上会告诉该人他猜到了URL。 如果你手动提高它,它将尝试渲染500页,我想要一个404。 那么有没有办法告诉Rails:“在所有情况下,你通常会渲染一个404.html或500.html ,渲染我的自定义404和500页”? (当然,我从public文件夹中删除了404.html和500.html页面。)

`open_http’:403 Forbidden(OpenURI :: HTTPError)用于字符串“Steve_Jobs”但不包含任何其他字符串

我正在浏览http://ruby.bastardsbook.com/上提供的Ruby教程,我遇到了以下代码: require “open-uri” remote_base_url = “http://en.wikipedia.org/wiki” r1 = “Steve_Wozniak” r2 = “Steve_Jobs” f1 = “my_copy_of-” + r1 + “.html” f2 = “my_copy_of-” + r2 + “.html” # read the first url remote_full_url = remote_base_url + “/” + r1 rpage = open(remote_full_url).read # write the first file to disk file = open(f1, “w”) file.write(rpage) file.close # […]