Rails display html tags - html

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>

Related

How to strip html tags from markdown without messing with links

I want to sanitize the following markdown in my Rails application:
<iframe>
<b>Title</b>
**Bold**
<http://example.com>
Exptected Result:
Title
**Bold**
<http://example.com>
Actual Result
Using the rails helper sanitize I get the following result where the <http://example.com> is stripped, because it seems to be considered a html tag:
Title
**Bold**
Question
How can I sanitize the text without interfering with the Markdown markup

ruby on rails datatype :text render in view

I try to use scaffolding in rails to build a blog. I save my blog's content as :text.
However, when I see the view. It can only show in one line. It looks not good.
How can I solve this problem?
Your database stores text, not HTML. In HTML line breaks are ignored unless they are marked up with relevant HTML tags. For example, a break between two paragraphs should be marked by the end of one <p> element and the beginning of another, and a single line break should be represented by a <br> tag.
Rails' simple_format helper converts text to HTML by replacing line breaks with HTML tags.
So instead of something like this:
<p><%= #post.content %></p>
You would do this:
<%= simple_format #post.content %>

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

How do I parse HTML in Rails?

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..