[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to save and display a newlinefeed with rails?

mrpink

5/2/2007 10:44:00 AM

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 -%>
<div class="blogentry">
<h3><%= h(entry.headline) %></h3>
<%= entry.news %> <br>
<%= link_to 'read comments',
{:action => 'read_comments', :id => entry },
:class => 'read_comments' %><br/>
</div>
<div class="separator">&nbsp;</div>
<% end %>

But the problem is that I have now textformatting inside the entry.news
field. E.g. how can I insert <br> 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
3 Answers

Alex Young

5/2/2007 10:50:00 AM

0

anansi wrote:
<snip>
> <%= entry.news %> <br>
<snip>
> But the problem is that I have now textformatting inside the entry.news
> field. E.g. how can I insert <br> after each line in entry.news?

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

or

<%= entry.news.gsub("\n", "<br />") %>

--
Alex

mrpink

5/2/2007 11:56:00 AM

0

arghh thanks for your help, wokrs great :)

Alex Young wrote:
> anansi wrote:
> <snip>
>> <%= entry.news %> <br>
> <snip>
>> But the problem is that I have now textformatting inside the
>> entry.news field. E.g. how can I insert <br> after each line in
>> entry.news?
>
> <%= entry.news.split("\n").join("<br />") %>
>
> or
>
> <%= entry.news.gsub("\n", "<br />") %>
>


--
greets

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


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

Mariusz Pekala

5/2/2007 8:52:00 PM

0

On 2007-05-02 21:00:05 +0900 (Wed, May), anansi wrote:
> arghh thanks for your help, wokrs great :)
>
> Alex Young wrote:
> >anansi wrote:
> ><snip>
> >> <%= entry.news %> <br>
> ><snip>
> >>But the problem is that I have now textformatting inside the
> >>entry.news field. E.g. how can I insert <br> after each line in
> >>entry.news?
> >
> ><%= entry.news.split("\n").join("<br />") %>
> >
> >or
> >
> ><%= entry.news.gsub("\n", "<br />") %>

Just my 2 cents, but I strongly recommend:

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

or

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

--
No virus found in this outgoing message.
Checked by "grep -i virus $MESSAGE"
Trust me.