selecting default value in a collection select

I'm having trouble selecting a default value in my collection select.

here is my code:

f.collection_select(:item_type_id, @item_types, :id, :name)

i want to select item_type.name = "N/A" for the default.

any ideas?

f.collection_select(:item_type_id, @item_types, :id, :name)

i want to select item_type.name = "N/A" for the default.

The f...:item_type_id pattern will set the collection to whatever the target
of the form_for has.

I think you don't want item_type_id to come back nil if the user selects
nothing. That calls for a :prompt => 'N/A' around the end

Warning: If the form_for points to an item with a filled-out item_type_id,
even if @item_types has no such item, you won't get a prompt.

Design Warning: Working with nils is evil. I would use a migration to add a
real 'N/A' to the item types, and I would park all unassigned item_type_id's
at it. That allows users to easily reset an item type back to 'N/A' without
you futzing around extra to provide a prompt. And without excessive nil
checks in all your item type code.

The f...:item_type_id pattern will set the collection to whatever the
target of the form_for has.

Geez - before my aphasia derails anyone...

The f...:item_type_id pattern will select the option matching whatever the
target of the form_for has.

I think you don't want item_type_id to come back nil if the user selects
nothing. That calls for a :prompt => 'N/A' around the end

I think you want item_type_id to come back nil if the user selects nothing.
That calls for a :prompt => 'N/A' around the end of the collection_select.

Remaining post _might_ be okay... :wink:

Phlip wrote:

f.collection_select(:item_type_id, @item_types, :id, :name)

i want to select item_type.name = "N/A" for the default.

The f...:item_type_id pattern will set the collection to whatever the
target
of the form_for has.

I think you don't want item_type_id to come back nil if the user selects
nothing. That calls for a :prompt => 'N/A' around the end

Warning: If the form_for points to an item with a filled-out
item_type_id,
even if @item_types has no such item, you won't get a prompt.

Design Warning: Working with nils is evil. I would use a migration to
add a
real 'N/A' to the item types, and I would park all unassigned
item_type_id's
at it. That allows users to easily reset an item type back to 'N/A'
without
you futzing around extra to provide a prompt. And without excessive nil
checks in all your item type code.

thanks for the reply! i just had to set

@item.item_type_id = "1"

in my new method in the controller. woohoo! i can finish coding now!

Scott Kulik wrote:

I'm having trouble selecting a default value in my collection select.

here is my code:

f.collection_select(:item_type_id, @item_types, :id, :name)

i want to select item_type.name = "N/A" for the default.

any ideas?

Use Like

f.collection_select(:item_type_id, @item_types, :id,
:name,:include_blank=>"N/A")

it will pass null value to controller and in front end page also "N/A"
will display