Selecting Range by Database Attribute for Rails Form -


is possible select range maximum number option pulled database? have code below gives 1 option. i'd give options 1 through @item.quantity.

<%= f.select(:quantity_requested, [1..@item.quantity], {}, { class: 'item-quantity form-control' }) %> 

all other examples see of hard coded numbers. appreciate understanding why doesn't work.

thanks!

i've never seen ruby's range dynamic numbers. tried doing in irb , didn't work correctly. alternative use (not tested, idea is):

<%= f.select(:quantity_requested, 1.upto(quantity.times).to_a, {}, { class: 'item-quantity form-control' }) %> 

edit: tried in irb console , started @ 0 based, you'll want use upto instead of times, see output below:

a = 100 a.times.to_a => [0, 1, ... 99]  1.upto(a).to_a => [1, 2, 3,.. 99, 100] 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -