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

Popular posts from this blog

python - matpltolib navigation toolbar edit curves and parameters line color automatically changes issue -

node.js - Nodejs javascript implementation of PBEWithMD5AndTripleDES/CBC/PKCS5Padding -