railslearner railslearner - 2 months ago 15
Ruby Question

How to insert iso inside option tag in rails

Hello I have included given code

<%= form.collection_select :country_id, available_countries, :id, :name, {}, {class: 'required'} %>


which gives me following list ::

<select class="required valid" name="order[bill_address_attributes][country_id]" id="order_bill_address_attributes_country_id">
<option value="1">Åland Islands</option>
<option value="2">Albania</option>
<option value="3">Algeria</option>
<option value="4">American Samoa</option>
<option value="5">Andorra</option>
<option value="6">Angola</option>
</select>


I need a list like this ::

<select class="required valid" name="order[bill_address_attributes][country_id]" id="order_bill_address_attributes_country_id">
<option value="1" iso = "AX">Åland Islands</option>
<option value="2" iso = "AL">Albania</option>
<option value="3" iso = "DZ">Algeria</option>
<option value="4" iso = "AD" >Andorra</option>
</select>


I fetch iso with
available_countries.map(&:iso)


Please guide me how to insert iso inside option tag.

Answer
f.select :country_id, options_for_select(@countries.map{ |c| [c.name, c.id, { 'data-currency_code' => c.currency_code }] })