Select method

The documentation states as an example:
select(“post”, “person_id”, Person.find_all.collect {|p| [ p.name, p.id
] },
{ :include_blank => true }) could become:

David Sam Tobias

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

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

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]}
) %>

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

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. 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 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.