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
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.
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
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?
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.