Hpricot results in an rhtml file

Hello,

I have the following code in an rhtml file as part of a rails app:

It is now <%= Time.now %>

<%
require ‘rubygems’
require ‘hpricot’
require ‘open-uri’

doc = Hpricot(open(“http://www.mass.gov/legis/184history/h00024.htm”))

puts doc
%>

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”))
-%>

<%= @doc %>

[email protected] wrote the following on 18.01.2007 19:04 :

It is now <%= Time.now %>

<%

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”))

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.

%>

Lionel.

On Thu, Jan 18, 2007 at 06:04:58PM -0000, [email protected] wrote:

It is now <%= Time.now %>

%>

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”))
# 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

It is now <%= Time.now %>

<%= processed_mass_gov_data %>

Thanks,
John
–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

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

Thanks for the help, obviously new to ROR.

On Thu, Jan 18, 2007 at 09:48:36PM -0000, [email protected] wrote:

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

Greg,

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

Thanks.

On Tue, Jan 23, 2007 at 08:18:17PM -0000, [email protected] wrote:

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.

On Jan 23, 4:00 pm, Gregory S. [email protected]