0

I am using rails 3.0 and I have an xml file where I store the content of my webpage. So for example, to fill in the body section of a given html page, I extract the content of the tag using REXML methods in ruby.

I would like to store a content with HTML tag inside this XML tag. Say, the following is my favorite content:

<body><strong>XXX</strong></body>

I am inserting this text in its escaped version so that XML parser doesn't interpret it as some content.

"&lt;strong&gt; XXX &lt;/strong&gt;"

Running seeds.rb file, I am reading this content to the database and eventually render it as an html page.

I tried many methods, I was unable to obtain what I want, namely: XXX

thanks for your help.

NoNameNo
  • 53
  • 1
  • 7
  • this site http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/ and this question http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html were helpful. – NoNameNo Apr 04 '11 at 00:38

3 Answers3

2

Another easy to output raw content

<%== @content.body %>

It's exactly the same as

<%= raw @content.body %>

it's just a shorthand method to output raw content

Tarellel
  • 869
  • 13
  • 17
0

try putting the html inside a CDATA section (unescaped):

<xmlNode>
    <![CDATA[
    <body><strong>XXX</strong></body>
    ]]>
</xmlNode>
manji
  • 47,442
  • 5
  • 96
  • 103
  • I already tried what you have suggested here. And no success. Actually, It doesn't work as long as the string is .html_safe declared in the controller. thx – NoNameNo Apr 04 '11 at 00:35
0

solved the problem, in my XML file, I have this content entered unescaped as it is:

<body> <strong>XXX</strong> </body>

in my seeds.rb file, I read the child of node with:

k.children[1].to_s

and finally in the controller, I declare the content as .html_safe:

<%= content_tag(:div, @content.body.html_safe) %>

NoNameNo
  • 53
  • 1
  • 7