How do I parse HTML in Rails? - html

I have a string with html tags in it saved.
=> "<p>hey man this is crazy g funk</p>\n<p>here i come with another crazy message from..</p>\n<p>dj eassssy d!##!.</p>"
How do you parse this so that it displays the way the HTML tags are implying?
I tried:
= Post.text
=h Post.text
= RedCloth.new(Post.text).to_html
= Hpricot(Post.text)

You want to do this:
<%= raw Post.text %>
or in haml
= raw Post.text
The reason is because rails escapes your html and will convert <p> into <p>.

Generally one parses html with html parsers. What do you mean "so that it displays the way the HTML tags are implying"? Displays on what? Presumably not a web browser..

Related

How to make a text bold in helper.rb ruby file inside a string

i have one mailer file called schedule_mailer.html.erb
in that I'm trying to use a helper method from that I'm rendering the contents
in scheudle_mailer.html.erb
<p style='font-size:15px;margin:0 25px 20px 25px' >
<%= body_text_tag() %>
</p>
in helper.rb
i need to send a string which includes <b> as well as <br>
but I'm receiving in mail as <b> and <br> tag itself.
my helper method
def body_text_tag()
body = "This email to inform you that"
if #creation
body + "your hotel schedule has been created <br>
<b>timings :</b> #{#timings}, <br>
<b>Room number :</b> #{#room number},<br>
....
....
....
elsif ....
.....
end.
as I have tried tag(:br), inside the string, even though it's rendering the mail with the tag(:br) itself, instead of the break line.
how to approach this kind of tags in the string to HTML?
The reason this doesn't work is that in Rails when you append a normal string to a safe buffer its automatically escaped to prevent XSS attacks.
irb(main):076:0> "<b>Hello".html_safe + "World</b>"
=> "<b>HelloWorld</b>"
If you want to create text that contains HTML tags from a helper you need to mark the portions that are HTML tags as safe:
"<b>".html_safe + #timings + "</b>".html_safe
Do not do:
"<b>#{ #timings }</b>".html_safe
Unless you actually know that the content is actually safe and doesn't contain user input.
This is what the tag helpers do automatically for you:
content_tag(:b, #timings)
tag.b(#timings)
But this code most likely doesn't actually belong in a helper in the first place. Use a view or a partial and write it in ERB.
one of the solutions which I tried and worked for me is to use html_safe, in the mailer.
as
<%= body_text_tag().html_safe %>

Rails display html tags

I would like to display html tags via Rails.
I'm using Rails 2.3
I've tried these ways
<p class="value"><%= raw #agent_event_monitor.name %></p>
<p class="value"><%= #agent_event_monitor.name.html_safe %></p>
<p class="value"><pre><code><%= #agent_event_monitor.name.html_safe %></code></pre></p>
but none doesn't give any result
on #agent_event_monitor.name is something like this
<b><p>Hello</p></b>
If you want HTML tags displayed, not rendered, in the browser, you need to escape them when printing them to your HTML document. Both raw and .html_safe do the opposite -- they unescape your HTML tags, ensuring that they are rendered in the browser.
To ensure your HTML tags are escaped, you need to use the h helper. The code would look like this:
<p class="value"><%= h #agent_event_monitor.name %></p>

How to get html to render on blog posts

I have a blog that saves input from html input tags, saves them as XML into a sqlite3 database, and then finally renders the content inside of this tag.
<p>
<b>Content:</b>
<%= #post.content %>
</p>
I need this .content to be able to render HTML. Any ideas?
For these kind of situations, you have to use either:
Markup language like RedCloth,
Or an editor like CKEditor.
Using CKEditor is a breeze with the CKEditor gem
And please note if you are using Rails 3, you need to explicitly say you want raw HTML, like this:
<p>
<b>Content:</b>
<%= raw #post.content %>
</p>

html.erb doesn't recognize html tags?

New to RoR, so please don't kill me ;)
Was wondering why does not Rails 3 recognize HTML tags retrieved from database?
For example,
Name Content
Title <b>Great</b> Show Edit Destroy
I wanted to have Content to be bold and put < b > tag around it, when it retrieves from a database it looks like a plain English.
Any thoughts?
Thank you in advance.
If I got this right, you need to do this:
<%= myrecord.content.html_safe %>
to get "real" html and not just escaped html code.
Even though it is unsafe to deliberately output HTML from the DB, you should call raw on the content you're trying to print.
<%= raw #object.my_content %>

interpreting escaped html

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.
"<strong> XXX </strong>"
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.
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
try putting the html inside a CDATA section (unescaped):
<xmlNode>
<![CDATA[
<body><strong>XXX</strong></body>
]]>
</xmlNode>
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) %>