I'm using
f.collection_select :country_id, Country.all, :id, :name)
which generates
<select name="user[country_id]" id="user_country_id">
<option value="1">Canada</option>
<option value="2">United Kingdom</option>
<option value="3" >United States</option>
</select>
I would like to include a prov-val and code-val attribute to the select so I can dynamically update the province labels:
<select name="user[country_id]" id="user_country_id">
<option prov-val="Province / Territory" code-val="Postal Code" value="1">Canada</option>
<option prov-val="County" code-val="Postcode" value="158">United Kingdom</option>
<option prov-val="State" code-val="ZIP Code" value="2" >United States</option>
Is this possible using a collection_select ?
Not sure if it's possible using collection_select, but I think using select does what you want:
<%= f.select :country_id, Country.all.map {|c| [c.name, c.id, {:'prov-val' => c.prov_val, :'code-val' => c.code_val}]} %>
This assumes that your country object has the prov_val and code_val fields.
You shouldn't be calling the model right from the view.
It is better to use an instance variable instead:
<%= f.select :country_id, @countries.map {|c|
[c.name, c.id, {:'prov-val' => c.prov_val, :'code-val' => c.code_val}]
} %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With