Forum: Ruby on Rails select method

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.
Gerard (Guest)
on 2006-04-06 21:18
(Received via mailing list)
The documentation states as an example:
select("post", "person_id", Person.find_all.collect {|p| [ p.name, p.id
] },
       { :include_blank => true }) could become:
<select name="post[person_id]">
  <option></option>
  <option value="1" selected="selected">David</option>
  <option value="2">Sam</option>
  <option value="3">Tobias</option>
</select>

I have the following code in a _form.rhtml file:
<%= select("category", "prnt_id", Prnt.find_all.collect {|p| [p.name,
p.id]},
{ :selected => 'prnt_id' == @category.prnt_id, :include_blank => false }
) %>

The drop down is created with the correct content, but is does NOT
select the
currently set value.  The value is set if I use any other type of input
(ie
text_field).

Is there an error in the doc?  If so, does anybody know how 'selected'
is set?

TIA
g
Mark Van H. (Guest)
on 2006-04-06 21:24
(Received via mailing list)
> currently set value.  The value is set if I use any other type of input
> (ie
> text_field).
>
> Is there an error in the doc?  If so, does anybody know how 'selected' is
> set?


Try it without the :selected option. I use this all over the palce and
that
is the only difference I see between yours and mine. Rails will call @
category.prnt_id for you in the select helper. Also just as an FYI,
:include_blank => false is the default setting.

<%= select("category", "prnt_id", Prnt.find_all.collect {|p| [p.name,
p.id]}
) %>
Dan P. (Guest)
on 2006-04-06 21:30
Gerard wrote:
> I have the following code in a _form.rhtml file:
> <%= select("category", "prnt_id", Prnt.find_all.collect {|p| [p.name,
> p.id]},
> { :selected => 'prnt_id' == @category.prnt_id, :include_blank => false }
> ) %>
>

Hey Gerard,

From the api:
By default, post.person_id is the selected option. Specify :selected =>
value to use a different selection or :selected => nil to leave all
options unselected.

Instead of :selected => value, you've got :selected => 'prnt_id' ==
@category.prnt_id

You should probably try this:

{ :selected => @category.prnt_id, :include_blank => false }

Dan
Gerard (Guest)
on 2006-04-06 23:34
(Received via mailing list)
Mark Van H. <mvette13@...> writes:
> I have the following code in a _form.rhtml file:
< <%= select("category", "prnt_id",
> :include_blank => false is the default setting.
>
> <%= select("category", "prnt_id", Prnt.find_all.collect {|p| [
> p.name, p.id]} ) %>-- Mark

Mark,

I have tried it that way too, and the way Dan Perez suggested after you,
but
still no joy:
<%= select("category", "prnt_id",
Prnt.find_all.collect {|p| [p.name, p.id] })
{ :include_blank => false } ) %>
or
<%= select("category", "prnt_id",
Prnt.find_all.collect {|p| [p.name,p.id]}))%>
or Dan's.  All do not seem to generate a selected= in the html code.

I'm using Rails 1.1.  How about you?

g
Mark Van H. (Guest)
on 2006-04-06 23:59
(Received via mailing list)
I am also using rails 1.1. Check to make sure that @category.prnt_id is
returning what you expect it should be returning. It may be returning
nil,
or a value which doesnt exist in the options for the select.

mark
Daniel (Guest)
on 2006-12-01 03:27
Mark Van H. wrote:
> I am also using rails 1.1. Check to make sure that @category.prnt_id is
> returning what you expect it should be returning. It may be returning
> nil,
> or a value which doesnt exist in the options for the select.
>
> mark

I'm having the same issue as Gerard, using Rails 1.1.6. I cannot get it
to select an option by default (or by specifying value to select).
Here's my code:

<%= select("thing", "category_id", Category.find_all.collect { |c|
[c.name, c.id] }) %>

Yes, @thing.category_id is what I'm expecting and it is a value in the
options.

Does this work at all?
Daniel (Guest)
on 2006-12-01 06:53
Daniel wrote:
> Mark Van H. wrote:
>> I am also using rails 1.1. Check to make sure that @category.prnt_id is
>> returning what you expect it should be returning. It may be returning
>> nil,
>> or a value which doesnt exist in the options for the select.
>>
>> mark
>
> I'm having the same issue as Gerard, using Rails 1.1.6. I cannot get it
> to select an option by default (or by specifying value to select).
> Here's my code:
>
> <%= select("thing", "category_id", Category.find_all.collect { |c|
> [c.name, c.id] }) %>
>
> Yes, @thing.category_id is what I'm expecting and it is a value in the
> options.
>
> Does this work at all?

I think I found the problem, actually. It looks like @thing.category_id
is being treated as a String while c.id is being treated as a Fixnum.
Annoying.
This topic is locked and can not be replied to.