Rails app中生成营业时间并显示当前状态的方法如下:
rails generate model BusinessHour day_of_week:integer opening_time:time closing_time:time
。class BusinessHour < ApplicationRecord
def self.open?
current_time = Time.now
current_day = current_time.wday
business_hour = find_by(day_of_week: current_day)
return false unless business_hour
opening_time = business_hour.opening_time
closing_time = business_hour.closing_time
current_time.between?(opening_time, closing_time)
end
end
if BusinessHour.open?
puts "当前店铺正在营业"
else
puts "当前店铺已关店"
end
这样,通过以上步骤,你可以在Rails app中生成营业时间并显示当前状态。对于更复杂的需求,你可以根据实际情况进行扩展和优化。
领取专属 10元无门槛券
手把手带您无忧上云