Non-breaking space in drop-down menu

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

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] + ’    ’ +
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.

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] + '    ' + 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

    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

    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] + '    ' + 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.

    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.

    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

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs