简体   繁体   English

Ruby on Rails下拉菜单已选择

[英]ruby on rails drop down menu selected

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. :select =>“某些值或文本”不适用于我。

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. 看到最后一个...它将列出我想要的所有PetStore名称,但是它始终默认为该列表中的名字。...即使“ Moe's Mammals”是另一个宠物商店的确切名称在下拉列表中。

the second f.select is just the same thing in a different syntax... but it also doesn't work. 第二个f.select只是相同的东西,只是语法不同……但是它也不起作用。 Even though :prompt => "whatever" will work fine. 即使:prompt =>“ whatever”也可以。 It leads me to believe that :selected doesn't mean anything to ruby 它使我相信:selected对红宝石没有任何意义

您好,只需在所选中使用ID即可。

 = f.select(:pet_store_id, PetStore.all.map {|p| [p.name,p.id]}, :selected => desired_pet_store.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM