On Wed, Jan 14, 2009 at 8:37 PM, Gu stav
<[email protected]> wrote:
>
> Hi,
>
> I'm trying to create a dropdown using collection_select. However, I cant
> seem to get it to select the right option by default.
>
> In my controller I have a setup like this:
>
> @content = Content.new
> @content.selection_id = 3
> @selections = Selection.find(:all)
>
> In my view I have:
>
> <% form_for :content, @content, :url => { :action => 'create' } do |f|
> %>
>    <%= f.collection_select :selection, @selections, :id, :name %>
>    <%= submit_tag 'Create' %>
> <% end %>

The first thing that sticks out is it should be :selection_id instead
of :selection.

> Despite me specifying the default value in the controller,
> collection_select doesnt seem to pick that up when rendering the html,
> which defaults to the first option in the dropdown.
>
> What idiot-simple thing am I missing? ;)

And if that doesn't fix it, then my only other suggestion would be to
make sure you have selection_id set as an integer in the database.
I've accidentally created a foreign key as a string before, which
works fine mostly, except when trying to do a select box and it's
comparing integers to strings.

Hope that helps.

-- Brandon
--------------------------------------------------------------------------------
Training by Collective Idea: Ruby on Rails training in a vacation setting
http://training.collectiveidea.com – San Antonio, TX – Jan 20-23

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to