Salil
Salil

Reputation: 47482

Showing only one space instead of more than one spaces in Rails

I am facing a problem of the spacing in a table values.

I have a simple table with a header Item Title and I am showing its value in it, but for the value 'Salil     Gaikwad' it is just showing Salil Gaikwad.

It means it is simply showing one space for more than one spaces when I retrieve it as <%= @item.item_title %> but I want to show it the same way as it is saved in the database i.e. Salil + 5 spaces + Gaikwad

Upvotes: 0

Views: 400

Answers (3)

Mr.Hunt
Mr.Hunt

Reputation: 4851

Easiest solution is to use the css white-space property on your html table. Since you want to preserve the white space, you can use the pre, pre-wrap, pre-line values as need.

See this link for reference: http://www.w3schools.com/cssref/pr_text_white-space.asp

table { white-space: <one of pre, pre-wrap, pre-line>; }

Upvotes: 0

Matteo Alessani
Matteo Alessani

Reputation: 10412

You could use the html tag pre around your text in the cell to preserve the spaces. Otherwise you can substitute the spaces with the html char &nbsp with something like this:

@item.item_title.gsub(/\s/, "&nbsp;")

Upvotes: 6

Greg Funtusov
Greg Funtusov

Reputation: 1447

It's an html thing.

Try to wrap the result in pre tags (

<%= ... %>
)

Or you can use the css white-space: property, using the needed selector.

Upvotes: 0

Related Questions