我对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如何将信息传递到我的视图中?
发布于 2010-08-25 04:46:27
我刚想通了!
控制器
@tickets = Hpricot(h.ticket_list)
@desc = (@tickets/:ticket)视图
<% @desc.each do |x| %>
<p><%=(x/:description)%></p>
<% end %>https://stackoverflow.com/questions/3560774
复制相似问题