我有这段代码:
= 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,但我没有看到所选的项目......我怎么能解决这个问题?
答案 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")