views:

28

answers:

1

Hi,

I recogn that I can use an options_from_collection_for_select inside a select box to display a list of options inside it. If I add (first.id), it will select the first option as default preselection. Example :

options_from_collection_for_select(@hauses, 'id', 'timebuild', @hauses.first.id)

However, if in the index.rhtml, I put this select box with the first option as default selection, everytime I refresh the page, it will still select again the first option, and not the current selection.

Please kindly guide, how can I change the parameter above, so when a user , example choosed option 2, then refresh the page, it does not change to option 1, but still in option 2. Also only for the first time page load, it will show option 1 (default).

Thank you

A: 

OK, so you want the select box to have a default value. Cool. But you shouldn't set it in the options_from_collection.

You should set it in the object that this id will be stored in.

The method signature is

options_from_collection_for_select(collection, value_method, text_method, selected = nil)

So, the selected should be your selected value. If you are doing something like

form_for @super_cool do |f|
  ...
  select_tag...
    options_from_collection_for_select(@hauses, 'id', 'timebuild', @super_cool.hause_id)

Also, the collection_select is much easier... in my example,

form_for @super_cool do |f|
  ...
  f.collection_select :hause_id, @hauses, :id, :timebuild
Jesse Wolgamott