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.).. <td><%= @event.event_detail.event_detail.gsub("\n", "<br>") %></td> 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. <td><%= puts @event.event_detail %></td>

Many thanks Valentino

Remove the puts and just use <%= @event.event_detail… %>

The ‘<%=’ is like puts

Andrew Timberlake http://ramblingsonrails.com

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

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 Timberlake 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 &nbsp; (non-breaking space) or set the table's style:

table {   white-space: pre; }

in your css.

Gav

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 "<table>" # concat is equivelant to puts in this case     for event in events do       concat "<tr>" # 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 "</tr>     end     concat "</table>"     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