Forum: Ruby on Rails select does not generate selected

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.
LeonB (Guest)
on 2007-03-15 00:45
(Received via mailing list)
Hi,

I have this code:
<%= debug @deposit.to_user_id %>
<%= select("deposit", "to_user_id", User.find_all.collect {|u|
[ u.fullname, u.id.to_s ] }, {:prompt => 'Select a user', :selected =>
@deposit.to_user_id} ) %>

It generates this:
--- 2
<select id="deposit_to_user_id" name="deposit[to_user_id]"><option
value="1">Leon B.</option>
<option value="2">Gijs Everaard</option></select>

Why doesn't it generate the prompt and the selected?

With kind regards,
Leon B.
LeonB (Guest)
on 2007-03-15 10:13
(Received via mailing list)
Anyone? If I change the to_user_id in user_id it works. But that
requires a change in the database and makes the whole just less
readable. Especially because there's another field called
from_user_id.
LeonB (Guest)
on 2007-03-15 21:50
(Received via mailing list)
Maybe there's anoyher way of achieving the same?
LeonB (Guest)
on 2007-03-15 22:29
(Received via mailing list)
Ahh... got it. This worked:
<%= collection_select("deposit", "to_user_id", User.find_all, "id",
"fullname", :selected => "to_user_id")  %>

But now :prompt does not work anymore....
<%= collection_select("deposit", "to_user_id", User.find_all, "id",
"fullname", :selected => "to_user_id", :prompt => "This is a test")
%>
LeonB (Guest)
on 2007-03-15 22:30
(Received via mailing list)
Ah....
This works:
<%= collection_select("deposit", "to_user_id", User.find_all, "id",
"fullname", {:prompt => 'Dit is een test'} )  %>

if to_user_id would not be "0". It's an integer and probably it's zero
to default. Can I change this behaviour? Else the selectlist won't
work. And setting it to '' before each selectlist seems like a crappy
solution.
LeonB (Guest)
on 2007-03-15 23:02
(Received via mailing list)
Ah got it.... Yes again....
table.column :to_user_id, :integer, :default => nil does it
This topic is locked and can not be replied to.