0

我对 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

我如何将信息传递到我的视图中?

4

1 回答 1

0

我刚刚想通了!

控制器

@tickets = Hpricot(h.ticket_list)
@desc = (@tickets/:ticket)

看法

<% @desc.each do |x| %>
    <p><%=(x/:description)%></p>
<% end %>
于 2010-08-24T20:46:26.990 回答