Forum: Ruby on Rails Can't get select helper to populate dropdown w/default value

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6cf3e18ebc948992a4cbe83330496e36?d=identicon&s=25 Peter Jagielski (sweetpete)
on 2006-01-01 18:00
Guys,

I searched this forum and found plenty of people having this problem,
but I didn't see any definitive solution.  Usings Rails 1.0.

When I edit an existing record, I can't get the basic "select" helper to
display the current value contained in the field of the record.  All of
my other (text) fields populate correctly with current field values.

There appears to be a ":selected =>" option with the select helper and
some of its variations, but I can't get it to work.  Frankly, I thought
it would simply "work", in the same way that text fields get populated
correctly.

Am I doing something wrong, is the select helper broken, or is it simply
not supposed to display the current value when editing?
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-01-01 18:57
(Received via mailing list)
Post some code for us to look at. There can be a lot of little things to
look at in these cases, and its tough to help with out seeing the code.

-Nick
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-01 19:10
Peter Jagielski wrote:
>
> Am I doing something wrong, is the select helper broken, or is it simply
> not supposed to display the current value when editing?

I suspect you are doing something wrong. If you use the select helpers,
they will automatically select the correct value for you if you have
them set up properly.

One thing that could do it is if the values displayed in your dropdown
do not match the values in your database.  For instance, if you are
displaying usernames, but storing user ids.

If that is the case, make sure you populate the option array properly
using something like this..

select("post", "person_id", Person.find_all.collect {|p| [ p.name, p.id
] }, { :include_blank => true })

The 'collect' being the important part here.  The first value is what
gets displayed, and the second is the internal value used by the select
dropdown.  In this case, the @post.person_id value should be an 'id',
not a 'name'.
6cf3e18ebc948992a4cbe83330496e36?d=identicon&s=25 Peter Jagielski (sweetpete)
on 2006-01-01 19:12
Nick,

Here's the hash that defines the contents of the dropdown:

YESNO = { "No" => "0",
          "Yes" => "1" }

In other words, the user sees options for "Yes" and "No, and either 0 or
1 are stored in the table's field.  In this case, the user can specify
whether or not they are an administrator.

Here's my select helper code:

<%= select 'user', 'administrator', User::YESNO.sort, :include_blank =>
TRUE %>

I tried all manner of including :selected => ??? and it's never worked.

Now, I have found the following workaround, which solves the problem by
using Rails' options_for_select helper:

<p><label for="user_administrator">Administrator</label><br/>
<select id="user_administrator" name="user[administrator]">
  <option value=""></option>
  <%= options_for_select User::YESNO.sort, @user.administrator.to_s %>
</select></p>

But going back to the original question, shouldn't the basic select
helper be able to handle this?
6cf3e18ebc948992a4cbe83330496e36?d=identicon&s=25 Peter Jagielski (sweetpete)
on 2006-01-01 19:27
Kevin,

I understand what you said, but I don't know if I need to use the
.collect the way you did, nor can I get it to work anyway.  Since the
data source for my dropdown is a hash and not the contents of a lookup
table, and the dropdown itself is being populated correctly, I'm not
sure if you your technique fits my situation.  Suggestions?
6cf3e18ebc948992a4cbe83330496e36?d=identicon&s=25 Peter Jagielski (sweetpete)
on 2006-01-01 19:33
Guys,

Ok, problem solved.  I changed my hash from:

YESNO = { "No" => "0",
          "Yes" => "1" }

to:

YESNO = { "No" => 0,
          "Yes" => 1 }

The field's type in my table is SMALLINT, so now the hash matches.
So...the following code works:

<%= select 'user', 'administrator', Bug::YESNO, :include_blank => TRUE
%>

Thanks Kevin & Nick!  Happy New Year!

Hope this helps the other folks getting this kind of thing to work!
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-01-01 19:33
(Received via mailing list)
Well thats the way I do it Peter, using the options helper that is. By
looking at the docs for the select helper, it doesn't need/have a
selected
value, it should automatically pick the right one. Like I said, I
usually
always use the options helper and never really had any problems with
that.

-Nick
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-02 07:09
Nick Stuart wrote:
> Well thats the way I do it Peter, using the options helper that is. By
> looking at the docs for the select helper, it doesn't need/have a
> selected
> value, it should automatically pick the right one. Like I said, I
> usually
> always use the options helper and never really had any problems with
> that.
>
> -Nick

incidentally, if you try to use the :prompt option for the select
helpers, make sure that your database table can accept a null for that
value.  The prompt is only selected by default when the corresponding
model value is a null.
This topic is locked and can not be replied to.