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

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -