'i want to add numbering at the beginning of the name in the dropdown list in ruby on rails

in my controller i have written like this

@agendas = @agendas.each{ |e| puts e.name }
      @agenda_alpha = @agendas.each_with_index.map { |agenda_key, i| ["#{i+1}.#{agenda_key}"] }

in my slim file i wrote this

label_method: ->(agenda) { @agenda_alpha },

This are the attributes in my rails console

i have to fetch the name attribute into the dropdown



Solution 1:[1]

It probably makes sense to translate the options into the required format in the controller or a helper first.

@options = %w[Apple Ball Cat]
@options = @options.each_with_index.map { |value, i| ["#{i}.#{value}", value] }
#=> [["0.Apple", "Apple"], ["1.Ball", "Ball"], ["2.Cat", "Cat"]]

Then just use this @options in your view:

f.select(:attribute_name, @options)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1