4
set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on

I tried this, content.gsub("\r\n","<br/>") but when I click the view/show button to see the contents of these line, I get the output/result=>

set tabstop=4<br/> set shiftwidth=4<br/> set nu<br/> set ai<br/> syntax on<br/> filetype plugin indent on

But I tried to get those lines as a seperate lines. But all become as a single line. Why?

How can I make all those lines with a html break (<br/>) ?

I tried this, that didn't work.

@addpost = Post.new params[:data]
@temptest = @addpost.content.html_safe
@addpost.content = @temptest
#logger.debug(@addpost)
@addpost.save

Also tried without saving into database. Tried only in view layer,<%= t.content.html_safe %> That didn't work too.

Got this from page source

        <a href="/posts/36">vimrc file</a> <br/>
        2011-12-06<br/><br/>

        set tabstop=4&lt;br/&gt;&lt;br/&gt;set shiftwidth=4&lt;br/&gt;&lt;br/&gt;set nu&lt;br/&gt;&lt;br/&gt;set ai&lt;br/&gt;&lt;br/&gt;syntax on&lt;br/&gt;&lt;br/&gt;filetype plugin indent on<br/>

            <a href="/posts/36/edit">Edit</a>
            <a href="/posts/36" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
        <br/><br/>
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
cola
  • 12,198
  • 36
  • 105
  • 165
  • For your information, `
    ` is the way to write it as of html5. You can still write `
    ` for xhtml compatibility, but it is not recommended. In fact, stick with `
    ` ;-)
    – Dominic Goulet Dec 06 '11 at 19:07
  • 1
    you might want to check out the `white-space: pre;` [css property](http://www.w3schools.com/cssref/pr_text_white-space.asp) and the
     tag. When using 
     tag, the only character you need to escape is the < and > character.
    – Lie Ryan Dec 06 '11 at 19:13
  • @Lie Ryan - I posted that answer some minutes ago. I believe it's the way to go! – Dominic Goulet Dec 06 '11 at 19:17
  • I edited the original post, i got this from page source, set tabstop=4<br/><br/>set shiftwidth=4<br/><br/>set nu<br/><br/>set ai<br/><br/>syntax on<br/><br/>filetype plugin indent on
    , it looks the angle bracket are being translated
    – cola Dec 06 '11 at 19:50

5 Answers5

26

An alternative to convert every new lines to html tags <br> would be to use css to display the content as it was given :

.wrapped-text {
    white-space: pre-wrap;
}

This will wrap the content on a new line, without altering its current form.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Dominic Goulet
  • 7,983
  • 7
  • 28
  • 56
6

You need to use html_safe if you want to render embedded HTML:

<%= @the_string.html_safe %>

If it might be nil, raw(@the_string) won't throw an exception. I'm a bit ambivalent about raw; I almost never try to display a string that might be nil.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I tried this, it didn't work, all lines as a single line, I changed oriniginal post, see the original post with code that i tried. – cola Dec 06 '11 at 18:59
  • @guru I don't see anything resembling `raw` or `html_safe` in what you posted. Once you've added the `
    ` tags, `raw` or `html_safe` is how you do it.
    – Dave Newton Dec 06 '11 at 19:00
  • @addpost.content.html_safe , and i also tried html.save function in show.html.erb , t.content.html.safe, that didn't work. – cola Dec 06 '11 at 19:38
  • @guru I don't understand what you're doing; normally you wouldn't save that to the DB, it's for the view layer. Good luck. – Dave Newton Dec 06 '11 at 19:44
  • @guru Can't explain it then--I don't see how it can't work. If you take a string "foo
    bar", expose it as an instance variable, `raw` or `html_safe` it, the HTML will be rendered.
    – Dave Newton Dec 06 '11 at 19:49
  • I edited the original post, i got this from page source, set tabstop=4<br/><br/>set shiftwidth=4<br/><br/>set nu<br/><br/>set ai<br/><br/>syntax on<br/><br/>filetype plugin indent on
    , it looks the angle bracket are being translated
    – cola Dec 06 '11 at 19:50
  • 2
    Yes, that worked in view layer, i had to do these, <% temptest2 = @post.content.gsub(/[\n]/,"
    ") %> <%= temptest2.html_safe %>
    – cola Dec 06 '11 at 20:11
  • @guru I'd consider putting most of that functionality either into a helper, into the model, into the controller, etc. rather than doing it all in the view--very messy there! – Dave Newton Dec 06 '11 at 20:18
5

With Ruby On Rails 4.0.1 comes the simple_format from TextHelper. It will handle more tags than the OP requested, but will filter malicious tags from the content (sanitize).

simple_format(t.content)

Reference : http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html

Dominic Goulet
  • 7,983
  • 7
  • 28
  • 56
3

http://www.ruby-doc.org/core-1.9.3/String.html as it says there gsub expects regex and replacement since "\n\r" is a string you can see in the docs:

if given as a String, any regular expression metacharacters it contains will be interpreted literally, e.g. '\d' will match a backlash followed by ‘d’, instead of a digit.

so you are trying to match "\n\r", you probably want a character class containing \n or \r -[\n\r]

a = <<-EOL
set tabstop=4
set shiftwidth=4
set nu
set ai
syntax on
filetype plugin indent on
EOL
print a.gsub(/[\n\r]/,"<br/>\n");
jackdoe
  • 1,846
  • 1
  • 15
  • 13
  • And still it's a single line on html web page, what i got, set tabstop=4

    set shiftwidth=4

    set nu

    set ai

    syntax on

    filetype plugin indent on
    – cola Dec 06 '11 at 19:35
1

I'm not sure I exactly follow the question - are you seeing the output as e.g. preformatted text, or does the source HTML have those tags? If the source HTML has those tags, they should appear on new lines, even if they aren't on line breaks in the source, right?

Anyway, I'm guessing you're dealing with automatic string escaping. Check out this other Stack Overflow question Also, this: Katz talking about this feature

Community
  • 1
  • 1
Matt
  • 10,434
  • 1
  • 36
  • 45