I’m using Rails 4.2.3. I have this select form field in my page …
<%= f.label :object %><br>
<div class="styled-select"><%= collection_select(:user_object, :object, @objects, :id, :description, {:prompt => true}) %></div>
My question is, how do I pre-select a value if there is a cookie present named “object”? I would like to set the value of the select menu to be the value of the cookie. Note, I only want to pre-select the value if this view is served by my controller’s “index” action (The above is part of a partial view that is served by different controller methods).
Thanks, - Dave
Hey according to docs at APIdock:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:selected => current_book.authors.map(&:id)})
So You should write it like this:
In You controller specify you objects: @selected = cookies[:some_key]
collection_select(:user_object, :object, @objects, :id, :description, {:selected => @selected.map(&:id), :prompt => true})
You do not need whole objects, just the ids
to check out selected ones. I think this should do the trick.