Initializing collection_select

I’m a noob. I’ve trolled the web and groups for help, but I just don’t
know enough Rails to apply the solutions to my particular problem. I
even got the screencast from Pragmatic Programmers, but still no dice.

I’m having a problem getting my collection_select to initialize with a
previously stored value. I have items and categories, with many items
to one category. When I edit an item, the category list populates
correctly, but I cannot figure out how to set the default to whatever
category was already stored for that item.

Running Rails 2.3.2, here is the code:

controller:
def edit
@observation = Observation.find(params[:id])
end

view:
<%= f.label “Category” %>

<%=
collection_select(:observation_category_id,
@category_id,
ObservationCategory.find(:all),
:id,
:name,
{:selected => @observation.observation_category_id})
%>

models:
class Observation < ActiveRecord::Base
attr_accessible :name, :icon_url, :observation_catategory_id
belongs_to :observation_category
end

class ObservationCategory < ActiveRecord::Base
has_many :observations
end

html:
Category

exercise

finance nutrition mood energy focus sleep junk_cat satiation

On Apr 7, 5:21 am, LukeG [email protected] wrote:

I’m a noob. I’ve trolled the web and groups for help, but I just don’t
know enough Rails to apply the solutions to my particular problem. I
even got the screencast from Pragmatic Programmers, but still no dice.

I’m having a problem getting my collection_select to initialize with a
previously stored value. I have items and categories, with many items
to one category. When I edit an item, the category list populates
correctly, but I cannot figure out how to set the default to whatever
category was already stored for that item.

collection_select is like the various model helpers in that you do
collection_select ‘instance_variable_name’, ‘method_name’, …
ie in this instance collection_select ‘observation’,
‘category_id’, …

Seeing as how you’ve already got a form_for setup, you should be able
to do f.collection_select ‘category_id’, …
Calling it on the form builder means you don’t need to tell rails
which object you are working with.

Fred

ive come across this problem and as far as my searching took me,
collection_select does not support the :selected option.
you can handle this by initializing the “selected” value in the
controller for the appropriate attribute before rendering the partial.

The code you have shown above is slightly flawed in the syntax of
collection_select as Fred has already pointed out. I think you want

<%= f.collection_select :observation_category_id,
ObservationCategory.find(:all), :id, :name %>

So in your case, the problem really was the syntax. The above should
work for your edit form ie. the correct option will be selected based
on the existing value (built into collection_select).

The lack of support for the :selected option in collection_select
however, is a side issue. Anyone know if there’s a fix for it? Besides
initializing the value for it in the controller before rendering the
partial?

On Apr 7, 12:43 pm, Frederick C. [email protected]

hey… thanx man…
i was wondering why my code wasn’t working…
well my problem was similar to yours, but a little bit different…
but you gave me a concrete solution to my problem through this
thread…
thanx…
good luck :slight_smile:

Thanks for the replies. The code, as y’all noted, was incorrect. This
fixed it:

collection_select(:observation,
         :observation_category_id,
        ObservationCategory.find(:all),
        :id,
        :name)

I read somewhere that if the :method parameter returns a value in
the :value parameter, that value will default to the selected item in
the list, and this turned out to be true, thus eliminating the need
for :selected.

If I understand your points, the f.collection_select syntax is
preferred over what I have. I’ll make that update, and thanks for the
advice.

-Luke