Champer Wu Champer Wu - 7 months ago 22
Ruby Question

Rails text field with jpg url

I have a Rails project for news board.

When the external link about image like :http://i.imgur.com/bn4B6Ko.jpg which I type in the text_field and submit, I get the view:


bla bla bla bla bla.... http://i.imgur.com/bn4B6Ko.jpg


html is as follow:

<p><%= @news.contents %></p>


I want to let it display image not just string and I try to add
<img src="">
tag in the text_field.

But I get view:


bla bla bla bla bla....
<img src="http://i.imgur.com/bn4B6Ko.jpg">



How can I make the image to display like this:


bla bla bla bla bla....

Answer

What you want to do is to render the string as HTML, and you are almost there.

To achieve this, one thing you can do is to specify it as a html_safe content. Something like the following:

<%= (@news.contents).html_safe %>

This should solve it for you.

PS: this is assuming that @news.contents contains everything needed to render html, i.e, the image tag is present there. For example:

@news.contents = '<img src="http://i.imgur.com/bn4B6Ko.jpg">'
<%= (@news.contents).html_safe %>

Hope this helps.

Comments