I want to set a default on a collection select. There must be a way to do this, but how?
Pale Horse wrote:
I want to set a default on a collection select. There must be a way to do this, but how?
:default => "kdjng" (?)
<%= collection_select(:category, :id, @categories, :id, :name, {},{:size=>10}) %> will have the value of @category selected so you can set the default value to @category and then use the collection select.
This was the method I used as I had to make different categories as default selected one for different users.There might be some better ways too.Eager to know them too.
Charanya Nagarajan wrote:
<%= collection_select(:category, :id, @categories, :id, :name, {},{:size=>10}) %> will have the value of @category selected so you can set the default value to @category and then use the collection select.
This was the method I used as I had to make different categories as default selected one for different users.There might be some better ways too.Eager to know them too.
Thanks for your response.
<%= f.collection_select :brand, ['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], :to_s, :to_s %>
The above is the syntax I'm currently using. In theory, the default brand would be Brand 1, correct? So when a form containing this collection gets submitted (without amending the collection), Brand 1 is parsed as the one selected. At least, that's what I'm looking for.
If you want to have a default selected value then you can use select_tag
<%= select_tag :brand, options_for_select(['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], "Brand 2") %>
You had used this <%= f.collection_select :brand, ['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], :to_s, :to_s %>
in case if the form f object is for article then u can use
<%= select_tag 'article[brand]', options_for_select(['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], "Brand 2") %>
Hope this helps.
Charanya Nagarajan wrote:
If you want to have a default selected value then you can use select_tag
<%= select_tag :brand, options_for_select(['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], "Brand 2") %>
You had used this <%= f.collection_select :brand, ['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], :to_s, :to_s %>
in case if the form f object is for article then u can use
<%= select_tag 'article[brand]', options_for_select(['Brand 1', 'Brand 2', 'Brand 3', 'Brand 4'], "Brand 2") %>
Hope this helps.
Thank you