Ruby on rails 在haml中查找两个日期之间的范围
我在haml方面没有经验,目前正在努力学习。下面是我的haml代码。当日期范围等于或大于4天时,我尝试显示一个按钮。基本上要求在距离结束日期只有4天时隐藏按钮Ruby on rails 在haml中查找两个日期之间的范围,ruby-on-rails,ruby,haml,Ruby On Rails,Ruby,Haml,我在haml方面没有经验,目前正在努力学习。下面是我的haml代码。当日期范围等于或大于4天时,我尝试显示一个按钮。基本上要求在距离结束日期只有4天时隐藏按钮 - range = booking.start_on..booking.end_on - if range >= 4 = link_to "#", class: 'btn-flat btn-flat-split', title: "Extend Booking", data: {
- range = booking.start_on..booking.end_on
- if range >= 4
= link_to "#",
class: 'btn-flat btn-flat-split',
title: "Extend Booking",
data: { method: request.primary_action.method } do
= "Extend Booking"
%i.icon-chevron-right
我得到了这个错误:
NoMethodError at /ideas/50114
undefined method `>=' for "06 Feb 2020".."20 Feb 2020":Range
我不能100%确定该范围是否得到正确的输出或声明正确。您拥有的是一个天数范围,它是一个范围对象,不实现
=
方法
您需要的是使用to_date
获取每个日期对象之间的差异天数:
p (booking.end_on.to_date - booking.start_on.to_date).to_i # 14
因此,对于您的情况:
- if (booking.end_on.to_date - booking.start_on.to_date).to_i >= 4
...
这是一个Ruby错误,不是Haml。我尝试了你的建议,得到了这个:
未定义的“2020年2月20日”的方法“-”:字符串是什么意思-@很抱歉,我在纯Ruby环境中尝试了这一点,但没有意识到这两个变量都是字符串。请参阅更新的答案。