Forum: Ruby on Rails initializing collection_select

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.
LukeG (Guest)
on 2009-04-07 09:33
(Received via mailing list)
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" %><br />
  <%=
    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:*
  <label for="observation_Category">Category</label><br />
  <select id="observation_category_id_" name="observation_category_id
[]"><option value="1">exercise</option>
<option value="2">finance</option>
<option value="3">nutrition</option>
<option value="4">mood</option>
<option value="5">energy</option>

<option value="6">focus</option>
<option value="7">sleep</option>
<option value="8">junk_cat</option>
<option value="9">satiation</option></select>
Frederick Cheung (Guest)
on 2009-04-07 09:44
(Received via mailing list)
On Apr 7, 5:21 am, LukeG <lgill...@gmail.com> 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
Ram (Guest)
on 2009-04-08 06:57
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
LukeG (Guest)
on 2009-04-08 18:21
(Received via mailing list)
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
Mike75 (Guest)
on 2009-05-31 09:48
(Received via mailing list)
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 :)
This topic is locked and can not be replied to.