DRD
DRD

Reputation: 131

rails select_tag selected no working

I'm not sure what I am doing wrong but my select_tag :selected is not working. Check the code below:

<%= select_tag :supplier, options_from_collection_for_select(SupplierItem.select("DISTINCT(SUPPLIER)").group("SUPPLIER"), "SUPPLIER", "SUPPLIER"), { :selected => params[:supplier], prompt: "Select a Category"} %>

Upvotes: 2

Views: 952

Answers (2)

DRD
DRD

Reputation: 131

I had the :selected => params[:supplier] in the wrong place. Working code below:

<%= select_tag :supplier, options_from_collection_for_select(SupplierItem.select("DISTINCT(SUPPLIER)").group("SUPPLIER"), "SUPPLIER", "SUPPLIER" ,:selected => params[:supplier]), {prompt: "Select a Category"}  %>

Upvotes: 3

smathy
smathy

Reputation: 27961

As per the docs, select_tag doesn't accept a :selected option because the <option> tags are all rendered by options_from_collection_for_select already by the time the select_tag is called.

So instead, as per the docs, options_from_collection_for_select takes, as it's optional fourth parameter, the selected value.

Upvotes: 0

Related Questions