Hpricot 和 Rails
我对 Ruby 和 Rails 完全陌生......事实上,我今天在 Rails 中创建了我的第一个应用程序,它发出 HTTP 请求来拉回 XML 文档,然后将其输出到屏幕......上手很简单......
嗯,我我现在需要解析 XML 字符串,但不知道如何使用 Hpricot 准确地做到这一点。
这是到目前为止我的代码
控制器
require 'hpricot'
class HelloController < ApplicationController
def index
h = Hello.new
@tickets = Hpricot(h.ticket_list)
end
end
模型
def ticket_list
url = URI.parse("http://example.com/test.xml")
req = Net::HTTP::Get.new(url.path)
req.basic_auth @@uname, @@pwd
res = Net::HTTP.new(url.host, url.port).start do |http|
http.request(req)
end
return res.body
end
我如何将信息传递到我的视图中?
I am completely new to Ruby and Rails... in fact I created my first application in Rails today that makes an HTTP request to pull back an XML document and then outputs it to the screen.. something simple to get started..
Well I am needing to now parse the XML string but am lost on how to do that exactly with Hpricot.
Here is my code so far
Controller
require 'hpricot'
class HelloController < ApplicationController
def index
h = Hello.new
@tickets = Hpricot(h.ticket_list)
end
end
Model
def ticket_list
url = URI.parse("http://example.com/test.xml")
req = Net::HTTP::Get.new(url.path)
req.basic_auth @@uname, @@pwd
res = Net::HTTP.new(url.host, url.port).start do |http|
http.request(req)
end
return res.body
end
How would I pass information into my view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚想通了!
控制器
视图
I just figured it out!
Controller
View