Champer Wu Champer Wu - 1 year ago 54
Ruby Question

Rails text field with jpg url

I have a Rails project for news board.

When the external link about image like : which I type in the text_field and submit, I get the view:

bla bla bla bla bla....

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="">

How can I make the image to display like this:

bla bla bla bla bla....


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="">'
<%= (@news.contents).html_safe %>

Hope this helps.