L
L
loogle182015-08-23 00:02:55
Ruby on Rails
loogle18, 2015-08-23 00:02:55

Who will tell you how to display the serial number in the element in ruby?

Good day!
There is a small problem (FE itself) with ruby:

= link_to 'blah-blah-blah', '#', class: 'some_class' do
  %span.order_number='Нужно вывести порядковый номер данной ссылки'

The task is to display its ordinal number inside the link. The link is taken from an array, to which I can’t reach in any way due to my knowledge of the ruby ​​language ((
The parshal itself is template and in some places the number of links can be different.
I don’t really want to do this on the frontend, because this is not a terrible thing and etc.
Can someone from the ruby ​​tell me how to implement this correctly in ruby?
I would be very grateful.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eugene Burmakin, 2015-08-23
@loogle18

Pure Ruby: apidock.com/ruby/Enumerable/each_with_index

- array.each_with_index do |element, index|
  = link_to 'blah-blah-blah', '#', class: 'some_class' do
    %span.order_number = index

D
Dima, 2015-08-23
@MAXOPKA

link_to is not Ruby, it's a helper from the Ruby on Rails framework.
If the array is iterated using the each method, then it can be replaced with .each_with_index, while the 2nd parameter will be passed to the block - a number from 0 to the number of elements in the array minus one. This parameter can serve as a serial number of the link.

R
Roman Mirilaczvili, 2015-08-23
@2ord

Isn't it easier to wrap it in a tag li? He will do his own thing.
Something like this

%ol
  - for link in links do
    %li
      = link_to link.path, class: 'some_class' do
        = link.name

T
thepry, 2015-08-24
@thepry

In some cases, it is beneficial to number using CSS, for example, if elements can be swapped, or if a particular partial can be obtained separately from the server and swapped on the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question