How to save and display a newlinefeed with rails?


A

anansi

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
 
Ad

Advertisements

A

Alex Young

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 <br> after each line in entry.news?

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

or

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

anansi

arghh thanks for your help, wokrs great :)

Alex said:
anansi wrote:



<%= 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
 
Ad

Advertisements

M

Mariusz Pêkala

--UPT3ojh+0CqEDtpF
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

arghh thanks for your help, wokrs great :)
=20

Just my 2 cents, but I strongly recommend:

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

or

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

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

--UPT3ojh+0CqEDtpF
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGOPnxsnU0scoWZKARAnmxAKCyBns2vdDNBvUnPQa+Pp/m/SidfgCgpt/h
dR4Vy9ic8YtYMn4n7A5zo4s=
=wsn5
-----END PGP SIGNATURE-----

--UPT3ojh+0CqEDtpF--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top