Default select option for select list in Rails -
here code checked:
f.select :engine_type_id, options_for_select(association_select_items(enginetype), selected: f.object.engine_type.id)
this code works good, if f.object.engine_type nil (for example, when object being created) got error "undefined method `id' nil:nilclass". how can fix it? thanks.
if it's new record or doesn't have engine type can't .id hence error. assuming it's belongs_to association have engine_type_id column.
so make it
selected: f.object.engine_type_id
or if using form_for , it's @car object can do
selected: @car.engine_type_id
Comments
Post a Comment