hpricot results in an rhtml file

Hello,

I have the following code in an rhtml file as part of a rails app: <html> <head>   <title></title> </head> <body>   <h1></h1> </body> <p>It is now <%= Time.now %></p> <p>

<% require 'rubygems' require 'hpricot' require 'open-uri'

doc = Hpricot(open("http://www.mass.gov/legis/184history/h00024.htm&quot;\))

puts doc %> </p> </html>

Hpricot is doing it's job and grabbing the mass.gov code, I can see it happening in the console. However, I can't get the mass.gov code to display on the rhtml page.

Is this even possible? What am I missing?'

Thanks, John

<% # you'd be better off putting this part in the controller and get it out of the view. require 'rubygems' require 'hpricot' require 'open-uri' @doc = Hpricot(open("http://www.mass.gov/legis/184history/h00024.htm&quot;\)) -%>

<%= @doc %>

Hello,

I have the following code in an rhtml file as part of a rails app: <html> <head>   <title></title> </head> <body>   <h1></h1> </body> <p>It is now <%= Time.now %></p> <p>

<% require 'rubygems' require 'hpricot' require 'open-uri'

doc = Hpricot(open("http://www.mass.gov/legis/184history/h00024.htm&quot;\))

puts doc %> </p> </html>

Hpricot is doing it's job and grabbing the mass.gov code, I can see it happening in the console. However, I can't get the mass.gov code to display on the rhtml page.

Is this even possible? What am I missing?'

It's certainly possible, it's just that puts doesn't do what you think it does. Also, most of that code should be in the helper, or possibly even in the controller. Consider this:

### helper file

require 'hpricot' require 'open-uri'

module FooHelper   def processed_mass_gov_data     doc = Hpricot(open("http://www.mass.gov/legis/184history/h00024.htm&quot;\))     # I assume you have reason to use Hpricot rather than just putting the     # retrieved HTML directly in the file, so this is where you'd do it     doc.to_html   end end

### view

<html> <head>   <title></title> </head> <body>   <h1></h1> </body> <p>It is now <%= Time.now %></p> <p> <%= processed_mass_gov_data %> </p> </html>

Thanks, John

--Greg

furfey@gmail.com wrote the following on 18.01.2007 19:04 :

Hello,

I have the following code in an rhtml file as part of a rails app: <html> <head>     <title></title> </head> <body>     <h1></h1> </body> <p>It is now <%= Time.now %></p> <p>

<%

use <%= instead of <% or nothing will be added to the page (the result of the block of code (if any) will be discarded).

require 'rubygems' require 'hpricot' require 'open-uri'

doc = Hpricot(open("http://www.mass.gov/legis/184history/h00024.htm&quot;\))

puts doc

here replace "puts doc" with "doc". puts ouputs on the standard output, not the page. If you replace it with "doc" only, it will be the result of the code block which is what you want.

%> </p> </html>

Lionel.

If I did just want to place the retrieved HTML directly, how would I do that?

Thanks for the help, obviously new to ROR.

If I did just want to place the retrieved HTML directly, how would I do that?

<%= open('http://blah…').read %>

Thanks for the help, obviously new to ROR.

--Greg

I assume you’re going to process and massage that retrieved HTML, right? 'Cause as soon as the browser sees the retrieved tag, I can’t imagine there wouldn’t be potential problems with the rest of your app [after the puts doc] showing up. And I’ll probably have nightmares about traversing the FrankenDOM!

RSL

Greg,

With this method, can I strip out certain tags, or have it just call certain tags?

Thanks.

Greg,

With this method, can I strip out certain tags, or have it just call certain tags?

You can do all sorts of magic with Hpricot. Just using open().read to splat it on the page won't do that, however.

It looks to me like you need to sit down with irb and figure out what steps you need to take to convert what you get from the original URL to what you actually want. Once you've gotten that code working dependably you can call it from the controller and put it in an instance variable, which you can refer to in the view.

I may be mistaken, but it looks to me like you are trying to use Rails without actually learning Ruby. This is a mistake, and will not serve you well. I recommend David Black's excellent book "Ruby for Rails" in this case, in addition to the pickaxe book.

Thanks.

--Greg

Thanks, and you're right. I will check out the books you recommended.