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?
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
Peter J. 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’.
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:
Administrator
<%= options_for_select User::YESNO.sort, @user.administrator.to_s %>
But going back to the original question, shouldn’t the basic select
helper be able to handle this?
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!
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?
Nick S. 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.
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