rails haml并选择带有id(class)的表单中的标签

时间:2014-03-11 21:31:54

标签: ruby-on-rails ruby-on-rails-4 haml

我有这段代码:

= f.select(:manufacturer_id, 
           VehicleManufacturer.all.map{ |c| [c.name, c.id]}, 
           :selected => f.object.manufacturer_id, 
           required: true, 
           id: "manufacturer-select")

我试着在我的选择框中设置一个ID或类,但它没有用,我也尝试了

= f.select(:manufacturer_id, 
           VehicleManufacturer.all.map{ |c| [c.name, c.id]}, 
           {:selected => f.object.manufacturer_id}, 
           required: true, 
           id: "manufacturer-select")
然后它工作了,我看到了正确的ID,但我没有看到所选的项目......我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:0)

根据Rails API文档:

select(method, choices, options = {}, html_options = {})
options_from_collection_for_select(collection, value_method, text_method, selected = nil)

所以,试试:

= f.select(:manufacturer_id, options_from_collection_for_select(VehicleManufacturer.all, :id, :name, @object.manufacturer_id ), {}, required: true, id: "manufacturer-select")