Eumundi
Eumundi

Reputation: 223

output a variable in rails containing html code Ruby on Rails

I have a little problem with an output in ruby and rails. I am still a beginner at rails, so it can be that the solution is pretty easy and i just can't see it.

I am trying to parse a website and put out some of the sourcecode on my own website. Problem: it always puts out the whole source code as a text and is not interpreting the html code. how can i change that?

code:

page = Nokogiri::HTML(open("https://www.google.ch/search?client=ubuntu&channel=fs&q=google&ie=utf-8&oe=utf-8&redir_esc=&ei=WMpyUfSWEuz07AaTioCwDw"))     

source = page.css('div#_css0')

<%=  page %>

result:

http://postimg.org/image/dsaib9lx3/

I want to it to look like:

http://postimg.org/image/z9qlhoef5/

Thanks for any suggestions!

Upvotes: 2

Views: 1798

Answers (2)

egor_hm
egor_hm

Reputation: 280

You should use raw in erb. It is actually equivalent to calling html_safe on it, but, just like h, is declared on a helper, so it can only be used on controllers and views.

page = Nokogiri::HTML(open("https://www.google.ch"))
<%= raw page %>

or

<%= raw Nokogiri::HTML(open("http://www.google.com")) %>

Upvotes: 3

mdesantis
mdesantis

Reputation: 8517

In Rails views every string content gets escaped, unless you use html_safe (docs)

Upvotes: 2

Related Questions