lundi 23 février 2015

Restrict the maximum quantity to select in a drop down to the value returned from a method

In my Ruby on Rails application I currently have the methods:



def remaining_seats
seats.count - bookings.sum(:seats_quantity)
end

def screens_info
"#{name} (#{remaining_seats}/#{seats.count} remaining)"
end


Which return the amount of seats that are available to be booked, for example if there have been two bookings which have booked 6 seats each in a 20 screen cinema it'll say there are 8/20 remaining.


What I want to do is use this so that in this drop down menu where the user selects the amount of seats they require:



<%= f.select :seats_quantity, '1'..'10' %>


Rather than having the range 1-10 the user can only select a quantity up to the amount of seats remaining. So, from the above example, if there are 8 seats available the user could select up to 8 seats from the drop down menu.


Ideally what I would like is to have a restriction of the maximum amount of seats being selected as 10, then if there are 9 available it would go to 9 or if there are only 3 seats available it would have the maximum of 3.


How can I do this?


Aucun commentaire:

Enregistrer un commentaire