我最近寫了一些輔助代碼,幫助我排除其中一家商店已經貼出營業時間爲星期幾。適應您的規格不應太難。
更新:增加了問題特定的代碼並更新了更有效的拒絕塊。
module HoursHelper
...
def generateDayStrings (unavailable_days)
all_days = [ ['Sunday', 'Sun'],
['Monday', 'Mon'],
['Tuesday', 'Tue'],
['Wednesday', 'Wed'],
['Thursday', 'Thu'],
['Friday', 'Fri'],
['Saturday', 'Sat'] ]
all_days.reject do |day|
(day - unavailable_days).size != 2
end
end
...
end
<%= f.select :day_of_week, generateDayStrings(@days_with_hours) %>
# @days_with_hours = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
# generateDayStrings(@days_with_hours) = [ ['Sunday', 'Sun'], ['Saturday', 'Sat'] ]
爲您的特定的代碼庫,它很可能是這個樣子:
module PaymentsHelper
...
def generatePaymentAmountSelectArray (unavailable_denoms)
all_denoms = [ ['$ 0', '0'],
['$ 1', '1'],
['$ 2', '2'],
['$ 5', '5'],
['$ 10', '10'] ]
# or all_denoms = Payment::AMOUNT_VALUES
# although not sure how this works with a hash.
all_denoms.reject do |current_denom|
(current_denom - unavailable_denoms).size != 2
end
end
...
end
<%= f.select :amount, generatePaymentAmountSelectArray(@used_values), {:blank => false}) %>
# just make sure you set @used_values in your controller
我覺得有這樣做的更好的辦法,所以,如果我還記得,我會更新它。
感謝您的輸入。我設法找到了一個似乎可以在我的模型中工作的單線程,使用:'.delete_if {| k,v | v