0

There are many posts about this... and I'm pretty sure my syntax is right, but for some reason

:select => "some value or text" is not working for me.

here is my form:

=form_for(@pet) do |f|
 =f.label :name
 =f.text_field :name, :placeholder => 'enter pet name...'
 %p
   =f.label :species
   =f.text_field :species, :placeholder => 'enter species name...'
 %p
   =f.label :color
   =f.text_field :color, :placeholder => 'enter color of pet...'
 %p
   =f.label :pet_store_id
   = f.collection_select(:pet_store_id, PetStore.all, :id, :name, {:selected => "Moe's Mammals"})

   = f.select(:pet_store_id, PetStore.all.map {|p| [p.name,p.id]}, :selected => "Moe's Mammals")

 %p
   =f.submit

See the last one...it will list all my PetStore names, which is what I want, but it always defaults to the first name in that list.... even though "Moe's Mammals" is the exact name of another pet store that is on the drop down list.

the second f.select is just the same thing in a different syntax... but it also doesn't work. Even though :prompt => "whatever" will work fine. It leads me to believe that :selected doesn't mean anything to ruby

rikkitikkitumbo
  • 954
  • 3
  • 17
  • 38

1 Answers1

0

Hi just use the id in selected.

 = f.select(:pet_store_id, PetStore.all.map {|p| [p.name,p.id]}, :selected => desired_pet_store.id)
Bot
  • 1,001
  • 2
  • 13
  • 32
  • At the moment, it looks like you are suggesting a string is used in the selected declaration, when it needs to be a PetStore#id. I'd suggest you edit that to :selected => desired_pet_store.id – ReggieB Jun 13 '14 at 21:43
  • Good suggestion. Although a string will also work but let's keep it perfect. – Bot Jun 13 '14 at 21:53
  • Thanks! stating the id instead of the name did it – rikkitikkitumbo Jun 14 '14 at 22:57