Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected option not working for select

I have this select which works fine, but default the select is empty and doesn't show the selected value (which is filled correctly):

<%= f.select(:relationgroup, options_for_select(@relationgroups), { :selected => @relation.relationgroup, :include_blank => true}) %>

Any idea why? Thanks!

like image 448
John Avatar asked Feb 13 '12 08:02

John


1 Answers

Try it that way:

<%= f.select(
        :relationgroup, 
        options_for_select(@relationgroups, @relation.relationgroup), 
        :include_blank => true
) %>

Not sure, but maybe it'll work better.

Anyway, assuming Relationgroup is some model with id and name (or any other attribute that you want to be visible in select options) attributes, and you're using default relationgroup_id foreign key in your model you'd better construct your select like that:

<% f.select(
       :relationgroup_id,
       options_from_collection_for_select(@relationgroups, :id, :name),
       :include_blank => true
) %>

It'll choose selected value based on object.relationgroup_id where object is the model you're building form for. See docs for more information.

like image 191
KL-7 Avatar answered Nov 12 '22 18:11

KL-7