A
A
Anton Misyagin2015-05-23 01:46:35
Ruby on Rails
Anton Misyagin, 2015-05-23 01:46:35

How to set custom html attributes for options for a simple_form dropdown list?

I'm using gem simple_form in the project. Generate dropdown list:

<%= f.input :region, :collection => GeoObjects.regions, 
    :label_method => lambda { |i| i.name },
    label: 'Регион' %>

I get html:
<select class="select required" required="required" aria-required="true" name="adv[region]" id="adv_region"><option value=""></option>
<option value="0">Московская область</option>
<option value="1">Ленинградская область</option>
<option value="2">Мордовия</option></select>

how to get additional attributes from options? You need latitude and longitude. You need to get the following:
<select class="select required" required="required" aria-required="true" name="adv[region]" id="adv_region"><option value=""></option>
<option value="0" lattitude="55.5" longitude="37.3">Московская область</option>
<option value="1" lattitude="54.53" longitude="34.345">Ленинградская область</option>
<option value="2" lattitude="53.3" longitude="34.3334">Мордовия</option></select>

I can set attributes on the select itself, but not on the option

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jeiwan, 2015-05-23
@sunnmas

It is necessary to form data for the list in a different way, collection:you need to pass in the parameter:

[
  ['Московская область', 0, { lattitude: 55.5, longitude: 37.3 }],
  ['Ленинградская область', 1, { lattitude: 54.53, longitude: 34.345 }]
  ...
]

That is, an array of arrays, the first element of which is the text for the option, the second is value, and the third is the attribute hash.
I'll leave the task of forming such an array to you :)

V
vsuhachev, 2015-05-23
@vsuhachev

through simple_form - either nothing or write your input with preference, etc.
Or use standard rail helpers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question