# Non-breaking space in drop-down menu

**URL:** <https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383>\
**Category:** Rails\
**Created:** [November 18, 2006, 1:17am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383 "2006-11-18T01:17:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 1:17am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/1 "2006-11-18T01:17:17Z")

</div>

I have a select box with the options stored in a constant in my model,  
e.g.  
TRANS\_CODES = [  
[“PM Payment – Regular”, “pm”],  
[“PX Payment – Regular, but skip periodic TA fee”, “px”],  
[“LP Late Payment – made after charges were posted”, “lp”],  
…

It works fine except that the little gap between, say, PM and the  
following explanation disappears. How can I create a non-breaking space  
between the codes and their explanations? Multiple spaces and tabs all  
reduce to a single space. I tried “PM” + “&nbsp” + … but all my  
attempts got syntax errors.

Help!

TIA,  
Shauna

---

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 3:20am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/2 "2006-11-18T03:20:53Z")

</div>

Shauna Haines wrote:

> I have a select box with the options stored in a constant in my model,  
> e.g.  
> TRANS\_CODES = [  
> [“PM Payment – Regular”, “pm”],  
> [“PX Payment – Regular, but skip periodic TA fee”, “px”],  
> [“LP Late Payment – made after charges were posted”, “lp”],  
> …
> 
> It works fine except that the little gap between, say, PM and the  
> following explanation disappears. How can I create a non-breaking space  
> between the codes and their explanations? Multiple spaces and tabs all  
> reduce to a single space. I tried “PM” + “&nbsp” + … but all my  
> attempts got syntax errors.
> 
> Help!
> 
> TIA,  
> Shauna

I haven’t had much luck playing with this and getting the select helper  
to do what you want. However, keep in mind that you don’t have to always  
use the helpers - you can build a SELECT of your own “by hand”, so to  
speak…

Say you change your TRAN\_CODES so it looks like this:

TRANS\_CODES = [  
[“PM”, “Payment – Regular”,  
“pm”],  
[“PX”, “Payment – Regular, but skip periodic TA fee”,  
“px”],  
[“LP”, “Late Payment – made after charges were posted”,  
“lp”]]

Then you could do something like this and it would look the way you  
want:

```
<select id="object_method" name="object[method]">
<% TRANS_CODES.each do |tc| %>
<option value="<%= tc[2] %>"<%= ' selected="selected"' if 

```

@object.method == tc[2] %\>\>\<%= tc[0] + ’&nbsp;&nbsp;&nbsp;&nbsp;’ +  
tc[1] %\>  
\<% end %\>

Replace “object” with the name of your model and “method” with the name  
of the attribute this field populates. If you have a lot of these, you  
could build your own helper to do it.

One big caveat - even getting it to work, unless you use a monospace  
font for the dropdown, the spaces are not going to line up perfectly  
like (I think) you want, and it’s going to look uneven.

c.

---

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 4:31am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/3 "2006-11-18T04:31:29Z")

</div>

Thanks, Cayce.

I altered my TRANS\_CODES definition as you suggested, then  
cut-and-pasted your select code into my new.rhtml view then replaced  
“object” with “payment” and “method” with “TransCode”. Here’s my result:

- \<% TRANS\_CODES.each do |tc| %\> \> \<%= tc[0] + '&nbsp;&nbsp;&nbsp;&nbsp;' + tc[1] %\> \<% end %\> 

But when I tried to run it, the Action Controller attempted to stick in  
.to\_s in a few places (maybe this is normal?) and got the following  
compile error:

… parse error, unexpected ‘)’, expecting kTHEN or ‘:’ or ‘\n’ or ‘;’  
if @payment.TransCode == tc[2] ).to\_s); \_erbout.concat “\>\n”  
^  
(the carat is pointing to the dot right before to\_s).

Can you see what I did wrong?

Thanks,  
Shauna

---

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 5:01am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/4 "2006-11-18T05:01:12Z")

</div>

Okay, that’s fixed, but now Action Controller complains about  
“uninitialized constant TRANS\_CODES”. I looked it over again and I’m  
pretty sure my syntax is as you outlined it. Here are the first few  
lines and the last two lines:

TRANS\_CODES = [  
[“PM”, “Payment – Regular”, “pm”],  
[“PX”, “Payment – Regular, but skip periodic TA fee”, “px”],

```
["XX", "Manual Entry", "xx"]

```

].freeze

Thanks again for helping,  
Shauna

---

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 4:52am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/5 "2006-11-18T04:52:08Z")

</div>

Shauna Haines wrote:

> Thanks, Cayce.
> 
> I altered my TRANS\_CODES definition as you suggested, then  
> cut-and-pasted your select code into my new.rhtml view then replaced  
> “object” with “payment” and “method” with “TransCode”. Here’s my result:
> 
> - \<% TRANS\_CODES.each do |tc| %\> \> \<%= tc[0] + '&nbsp;&nbsp;&nbsp;&nbsp;' + tc[1] %\> \<% end %\> 
> 
> But when I tried to run it, the Action Controller attempted to stick in  
> .to\_s in a few places (maybe this is normal?) and got the following  
> compile error:
> 
> … parse error, unexpected ‘)’, expecting kTHEN or ‘:’ or ‘\n’ or ‘;’  
> if @payment.TransCode == tc[2] ).to\_s); \_erbout.concat “\>\n”  
> ^  
> (the carat is pointing to the dot right before to\_s).
> 
> Can you see what I did wrong?
> 
> Thanks,  
> Shauna

I think the problem is you are breaking an inline if statement across  
two lines.

Where you have:

\>

You need to keep the if statement and what it applies to all together on  
one line without a break:

\<%= ’ selected=“selected”’ if @payment.TransCode == tc[2] %\>

c.

---

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 5:24am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/6 "2006-11-18T05:24:28Z")

</div>

Shauna Haines wrote:

> Sorry, I did find my last problem:
> 
> I needed to precede TRANS\_CODES with Payment::
> 
> And you are correct, in a non-monotype font the spaces aren’t exactly  
> right. But I can fiddle with that. Thanks for getting me past the  
> logjam!
> 
> Shauna

Yep, you got it - Payment::TRANS\_CODES - sorry about that it didn’t  
cross my mind.

Glad to help. Bedtime.

c.

---

<div class="post-metadata">

**Author:** ![Shauna\_Haines](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9d8465/32.png) [@Shauna\_Haines](https://www.ruby-forum.com/u/Shauna_Haines)\
**Post date:** [November 18, 2006, 5:06am UTC](https://www.ruby-forum.com/t/non-breaking-space-in-drop-down-menu/76383/7 "2006-11-18T05:06:00Z")

</div>

Sorry, I did find my last problem:

I needed to precede TRANS\_CODES with Payment::

And you are correct, in a non-monotype font the spaces aren’t exactly  
right. But I can fiddle with that. Thanks for getting me past the  
logjam!

Shauna
