Display a string properly in View

Dear all

I have problem in print the string in View.

@event = Event.first

puts @event.event_detail
dreq_reqno dreq_status dreq_create_time
09P0917228 0 18 Jun 2009 16:32
09P5075748 0 18 Jun 2009 16:32
09P5075755 0 18 Jun 2009 16:33
The format is pretty good using puts

But when I print this in View, the format disorder (Some spaces are
trimmed.)…

<%= @event.event_detail.event_detail.gsub("\n", "
") %> dreq_reqno dreq_status dreq_create_time 09P0917228 0 18 Jun 2009 16:32 09P5075748 0 18 Jun 2009 16:32 09P5075755 0 18 Jun 2009 16:33

How can I have the “puts @event.event_detail” in View?? I tried this,
but not work.

<%= puts @event.event_detail %>

Many thanks
Valentino

On Thu, Jun 18, 2009 at 11:12 AM, Valentino L. <
[email protected]> wrote:

09P5075748 0 18 Jun 2009 16:32

How can I have the “puts @event.event_detail” in View?? I tried this,
but not work.

<%= puts @event.event_detail %>

Many thanks
Valentino

Remove the puts and just use <%= @event.event_detail… %>
The ‘<%=’ is like puts

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings

Hi Valentino

You shouldn’t use puts, for output in the view.

You probably want to split the info for each event into seperate cells
so your table is well-structured.
In which case I’d write a helper_method and throw most of the work
into there.

If not, you can stop html from collapsing white-space by either
replacing each space with   (non-breaking space) or set the
table’s style:

table {
white-space: pre;
}

in your css.

Gav

On Jun 18, 10:12 am, Valentino L. [email protected]

Dear Gavin

Thanks for your reply, it works well after amend the css file.

But I don’t understand how the helper_method can use to generate a
table?? Could you further elaborate on it? (Sorry that I am new in
rails…)

Many thanks
Valentino

Gavin M. wrote:

Hi Valentino

You shouldn’t use puts, for output in the view.

You probably want to split the info for each event into seperate cells
so your table is well-structured.
In which case I’d write a helper_method and throw most of the work
into there.

If not, you can stop html from collapsing white-space by either
replacing each space with   (non-breaking space) or set the
table’s style:

table {
white-space: pre;
}

in your css.

Gav

http://handyrailstips.com

On Jun 18, 10:12�am, Valentino L. [email protected]

Thank you for your reply

It is not work using this
<%= @event.event_detail %>

it seems that the “\n” is not working in HTML, and the multiple space
are trimmed

event.event_detail.inspect
=> “"dreq_reqno dreq_status dreq_create_time\n 09P0917228
0 18 Jun 2009 16:32\n 09P5075748 0 18 Jun 2009 16:32\n
09P5075755 0 18 Jun 2009 16:33\n"”

Many thanks
Valentino

Andrew T. wrote:

On Thu, Jun 18, 2009 at 11:12 AM, Valentino L. <
[email protected]> wrote:

09P5075748 0 18 Jun 2009 16:32

How can I have the “puts @event.event_detail” in View?? I tried this,
but not work.

<%= puts @event.event_detail %>

Many thanks
Valentino

Remove the puts and just use <%= @event.event_detail… %>
The ‘<%=’ is like puts

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings

Sure,

From what you’ve written, it looks like your event model has
attributes called dreq_reqno, dreq_status and dreq_create_time and
you’ve joined these together with the event_detail method.

If this is the case, you could add a method in on of your helper files
(probably EventsHelper) that will build a table for you with all of
the desired columns and rows.

Here’s an example:

def events_table(events)
concat “

” # concat is equivelant to puts in this case
for event in events do
concat “” # you can write html tags or use the rails view
helpers
concat content_tag(:td, event.attribute_1) # the first columns
attribute
concat content_tag(:td, event.attribute_2) # the second columns
attribute
concat "
end
concat “

nil # important to return nil at the end of the helper method
end

Writing html in a helper using the concat method can get a little
messy though. I would use a gem/plugin called markaby here as it’s
much cleaner.

when adding a table in your view, you’d simply use <%= events_table
(@events) %>

Thinking about it, this would probably be easier in a parial:

parial called events

<% content_tag :table do %>
<% for event in events do %>
<% content_tag :tr do %>
<%= content_tag :td, event.attribute_1 %>
… etc
<% end %>
<% end %>
<% end %>

Then call <%= render :partial => ‘events’, :object => @events %> from
your view.

Gav