<%= collection_select(:user_ob" /> <%= collection_select(:user_ob" /> <%= collection_select(:user_ob"/>

HOw do I pre-select a value in my select menu if a cookie is set?

498 views Asked by At

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

5

There are 5 answers

0
WRK On BEST ANSWER

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.

1
Shani On

If you want some object to be selected by default, you can use :selected

assuming that your cookie object stores an id

{:selected => object}

<%= collection_select(:user_object, :object, @objects, :id, :description, {:prompt => true}, {:selected => object} ) %>
1
Albert Paul On

Get cookie value in your Index controller,

def index
  @selected_object = cookies[:some_key] 
end

In your Index.html.erb

<div class="styled-select"><%= collection_select(:user_object, :object, @objects, :id, :description, {:prompt => true}, {selected: <%= @selected_object %> }) %></div>

Or you can get that cookie on the fly from View file,

<div class="styled-select"><%= collection_select(:user_object, :object, @objects, :id, :description, {:prompt => true}, {selected: <%= cookies[:some_key] %> }) %></div>

Note that value of cookie might be hash, make sure you get the correct value, also you can use conditional in controller to avoid getting nil and resulting no value selected in view by,

def index
  @selected_object = cookies[:some_key]
  if @selected_object.nil?
    @selected_object = "default_selected"
  end
end
0
dp7 On

If you want some object to be selected by default, be sure to use its id, not the whole object.

Try the following:

<%= f.collection_select(:user_object,:object,@objects ,:id, :description,:selected => cookies[:object])%>

Note: Make sure cookies[:object] returns an id of the items in the select list

Source - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select#632--selected

2
toddmetheny On

I like to use options_for_select. Options for select can take an array or a two dimensional array, in the latter case the first value is shown to the user and the second value is what is saved. It can also take an optional second argument that selects the default value:

if cookies[:whatever_key_is]
  <%= f.select(:object, options_for_select(["array", "of", "options"], cookies[:whatever_key_is]) %>
end

I usually write a method for passing the array, especially if it's two dimensional, and then call that method as the first argument. I actually also usually write a method for the second argument.