Creating a select tag with rails with a range of numbers in steps

What is the easiest way to write a select rails tag that will generate a selection for numbers 1-15, step by step? 5?

I know that this should be simple, but I'm struggling with the syntax, I know that there should be a more elegant way to write it than the way I did it.

thanks!

+6
source share
3 answers
select_tag "sizes", options_for_select((4..15).step(0.5)) 

This is how I wrote it ... but is this the best way to get closer?

also if i wanted 4.0 / 5.0 / 6.0 etc. read 4/5/6 as integers, is there any way to do this / enable this on one line?

+15
source
 select_tag "sizes", options_for_select( (4..15).step(0.5).map{|n| n%1 == 0 ? n.to_i : n} ) 
+2
source

The options_for_select method does what you want, but only if you can seed it with the correct data. This means that you need an array with the corresponding values ​​in it. For instance:

 options_for_select((0..28).to_a.collect { |v| v.to_f / 2 + 1 }) 
0
source

Source: https://habr.com/ru/post/919248/


All Articles