简体   繁体   中英

Hpricot and Rails

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?

I just figured it out!

Controller

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

View

<% @desc.each do |x| %>
    <p><%=(x/:description)%></p>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM