检查源是否存在
我正在用 Rails 编写一个网站,并且需要查看 RSS 提要。该脚本按预期工作,但我刚刚收到一条错误,表明无法读取源代码。
这是脚本:
def setup_feed
source = "http://path_to_feed/"
content = "" # raw content of rss feed will be loaded here
open(source) do |s|
content = s.read
end
@rss = RSS::Parser.parse(content, false)
end
我担心的是,如果源出于某种原因不可用,该站点将产生错误或只是“崩溃”。我该如何保护自己免受这种情况的侵害?
这是确切的错误:
Errno::ENOENT in WelcomeController#index
No such file or directory - http://path_to_feed/
I'm writing a website in Rails and I need to reed a RSS feed. The script works as intended, but I just got an error indicating that the source could not be read.
This is the script:
def setup_feed
source = "http://path_to_feed/"
content = "" # raw content of rss feed will be loaded here
open(source) do |s|
content = s.read
end
@rss = RSS::Parser.parse(content, false)
end
My concern is that the site will produce an error or just "crash" if the source isn't available for whatever reasons. How can I protect myself against this?
This is the exact error:
Errno::ENOENT in WelcomeController#index
No such file or directory - http://path_to_feed/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
找到了更新的链接:http://binarysoul.com/blog/rails-3-url -Eric Himmelreich 的验证
EDIT
Found a more recent link: http://binarysoul.com/blog/rails-3-url-validation by Eric Himmelreich
通常,您需要在后台处理这样的外部请求。
这里提供一个简单的示例有点复杂,但主要步骤是:
这样,页面加载仅依赖于您自己的数据库,而不依赖于您可能无法控制的任何外部资源。
Generally, you'll want to handle external requests like this in the background.
It's a bit too involved to provide a simple example here, but the main steps are:
This way, page loads are dependent only on your own database, not on any external resources that you may not control.