I have this code:
%tr
%th Parent
%td
%select{ :name => "firstlevel_id", :value => "#{@s.firstlevel_id}" }
- Firstlevel.find(:all).each do |f|
%option{ :value => "#{f.id}" } #{f.title}
but even if the firstlevel_id
is already one of the id's in the options list, it doesn't show it as selected.
select_tag
helper to create select
tag and control selected value. You can also set selected
attribute on proper option
tag:
%select{:name => "firstlevel_id"} - FirstLevel.find(:all).each do |f| %option{:value => f.id, :selected => f.id == @s.firstlevel_id} #{f.title}
I would prefer the first solution.
您应该使用select_tag生成html,并使用options_for_select生成html:
select_tag :firstlevel_id, options_for_select( Firstlevel.all.map{|l| [l.title, l.id] }, @s.try(:firstlevel_id) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.