# Display a string properly in View

**URL:** <https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508>\
**Category:** Rails\
**Created:** [June 18, 2009, 11:12am UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508 "2009-06-18T11:12:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 18, 2009, 11:12am UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508/1 "2009-06-18T11:12:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 18, 2009, 11:20am UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508/2 "2009-06-18T11:20:55Z")

</div>

On Thu, Jun 18, 2009 at 11:12 AM, Valentino L. \<  
removed\_email\_address@domain.invalid\> 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://ramblingsonrails.com)

[http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 18, 2009, 11:26am UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508/3 "2009-06-18T11:26:48Z")

</div>

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

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

On Jun 18, 10:12 am, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 18, 2009, 11:36am UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508/4 "2009-06-18T11:36:38Z")

</div>

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 &nbsp; (non-breaking space) or set the  
> table’s style:
> 
> table {  
> white-space: pre;  
> }
> 
> in your css.
> 
> Gav
> 
> [http://handyrailstips.com](http://handyrailstips.com)
> 
> On Jun 18, 10:12ï¿½am, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 18, 2009, 11:25am UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508/5 "2009-06-18T11:25:08Z")

</div>

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. \<  
> removed\_email\_address@domain.invalid\> 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://ramblingsonrails.com)
> 
> [http://MyMvelope.com](http://MyMvelope.com) - The SIMPLE way to manage your savings

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [June 18, 2009, 12:00pm UTC](https://www.ruby-forum.com/t/display-a-string-properly-in-view/170508/6 "2009-06-18T12:00:53Z")

</div>

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.

> **[\#69 Markaby in Helper - RailsCasts](http://railscasts.com/episodes/69-markaby-in-helper)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

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
