Rails Select in Form returns Index. I want to send

2019-08-07 16:09发布

问题:

<%= f.select :owner, options_for_select(names) %>

Here names is an array of names e.g. "harry", "barry", "joe". The form element sets value of :owner to the index of the selected option i.e. 0,1,2. I want to send the value instead i.e. "harry", "barry", "joe".

Is there an option for select that will do so? If not, how can I achieve this?

回答1:

Map name in a two element array of [text,value] pairs thus:

<%= f.select :owner, options_for_select(names.map {|name| [name,name]}) %>