# How to save and display a newlinefeed with rails?

**URL:** <https://www.ruby-forum.com/t/how-to-save-and-display-a-newlinefeed-with-rails/103148>\
**Category:** Ruby\
**Created:** [May 2, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/how-to-save-and-display-a-newlinefeed-with-rails/103148 "2007-05-02T12:45:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [May 2, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/how-to-save-and-display-a-newlinefeed-with-rails/103148/1 "2007-05-02T12:45:57Z")

</div>

Hi,  
in my little rails application I’ve made a little database (create.sql)  
like this:

drop table if exists entries;

create table entries (  
id int not null auto\_increment,  
headline varchar(100) not null,  
news text not null,  
date\_available datetime not null,  
primary key (id)  
);

You see the news which is a text-type. Scaffold did a nice template with  
these infos where I can type in these data and save it. After that I can  
print these entries on the screen again with this \*.rhtml:

\<% for entry in @entries -%\>

  

### \<%= h(entry.headline) %\>
  
\<%= entry.news %\>   
  
\<%= link\_to ‘read comments’,  
{:action =\> ‘read\_comments’, :id =\> entry },  
:class =\> ‘read\_comments’ %\>  
  

  

&nbsp;
  
\<% end %\>

But the problem is that I have now textformatting inside the entry.news  
field. E.g. how can I insert   
 after each line in entry.news?

–  
greets

```
                        (
                        )
                     (
              /\ .-"""-. /\
             //\\/ ,,, \//\\
             |/\| ,;;;;;, |/\|
             //\\\;-"""-;///\\
            // \/ . \/ \\
           (| ,-_| \ | / |_-, |)
             //` __\.-.-./__ `\\
            // /.-(() ())-.\ \\
           (\ |) '---' (| /)
            ` (| |) `
      jgs \) (/

```

one must still have chaos in oneself to be able to give birth to a  
dancing star

---

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [May 2, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/how-to-save-and-display-a-newlinefeed-with-rails/103148/2 "2007-05-02T12:50:59Z")

</div>

anansi wrote:

> ```
> <%= entry.news %> <br>
> 
> ```

\> But the problem is that I have now textformatting inside the entry.news \> field. E.g. how can I insert   
 after each line in entry.news?

\<%= entry.news.split("\n").join("  
") %\>

or

\<%= entry.news.gsub("\n", “  
”) %\>

---

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [May 2, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/how-to-save-and-display-a-newlinefeed-with-rails/103148/3 "2007-05-02T14:01:10Z")

</div>

arghh thanks for your help, wokrs great 🙂

Alex Y. wrote:

> or
> 
> \<%= entry.news.gsub("\n", “  
> ”) %\>

–  
greets

```
                        (
                        )
                     (
              /\ .-"""-. /\
             //\\/ ,,, \//\\
             |/\| ,;;;;;, |/\|
             //\\\;-"""-;///\\
            // \/ . \/ \\
           (| ,-_| \ | / |_-, |)
             //` __\.-.-./__ `\\
            // /.-(() ())-.\ \\
           (\ |) '---' (| /)
            ` (| |) `
      jgs \) (/

```

one must still have chaos in oneself to be able to give birth to a  
dancing star

---

<div class="post-metadata">

**Author:** ![anansi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9fc348/32.png) [@anansi](https://www.ruby-forum.com/u/anansi)\
**Post date:** [May 2, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/how-to-save-and-display-a-newlinefeed-with-rails/103148/4 "2007-05-02T22:52:20Z")

</div>

On 2007-05-02 21:00:05 +0900 (Wed, May), anansi wrote:

> > \<%= entry.news.split("\n").join("  
> > ") %\>
> > 
> > or
> > 
> > \<%= entry.news.gsub("\n", “  
> > ”) %\>

Just my 2 cents, but I strongly recommend:

\<%= CGI::escapeHTML(entry.news).gsub("\n", “  
”) %\>  
\<%= CGI::escapeHTML(entry.news).split("\n").join("  
") %\>

or

\<%= h(entry.news).gsub("\n", “  
”) %\>  
\<%= h(entry.news).split("\n").join("  
") %\>
