如何在 Helper for CSS 中 == 建模方法 Class?
How to == Model Method Class in Helper for CSS?
calendar_helper
def day_classes(day)
classes = []
classes.empty? ? nil : classes.join(" ")
classes << "future" if day > Date.today # This make future days a white background, by default days are blue background
# The Below Line Gives an Error (I want missed_dates date_missed to be red background):
classes << "missed" if day == current_user.missed_dates.group_by {|i| i.date_missed.to_date}
end
页面中的名称错误#home
undefined local variable or method 'current_user'
css
.future { background-color: #FFF; }
.missed { background-color: red; }
我用 railscasts tutorial 建立了日历。
将此行添加到您的 Calendar
class:
delegate :current_user, to: :view
由于 SessionsHelper
自动包含在视图中,它应该能够将您的 view
变量传递给您的 Calendar
class.
Thank you that removed the error, but for some reason the days that I marked as date_missed didn't turn red
我猜 current_user.missed_dates.group_by {|i| i.date_missed.to_date}
是一个错过日期的数组,因此,为了检查您的日期是否在该数组中,请使用 include?
:
missed_dates = current_user.missed_dates.group_by {|i| i.date_missed.to_date}
classes << "missed" if missed_dates.include?(day)
calendar_helper
def day_classes(day)
classes = []
classes.empty? ? nil : classes.join(" ")
classes << "future" if day > Date.today # This make future days a white background, by default days are blue background
# The Below Line Gives an Error (I want missed_dates date_missed to be red background):
classes << "missed" if day == current_user.missed_dates.group_by {|i| i.date_missed.to_date}
end
页面中的名称错误#home
undefined local variable or method 'current_user'
css
.future { background-color: #FFF; }
.missed { background-color: red; }
我用 railscasts tutorial 建立了日历。
将此行添加到您的 Calendar
class:
delegate :current_user, to: :view
由于 SessionsHelper
自动包含在视图中,它应该能够将您的 view
变量传递给您的 Calendar
class.
Thank you that removed the error, but for some reason the days that I marked as date_missed didn't turn red
我猜 current_user.missed_dates.group_by {|i| i.date_missed.to_date}
是一个错过日期的数组,因此,为了检查您的日期是否在该数组中,请使用 include?
:
missed_dates = current_user.missed_dates.group_by {|i| i.date_missed.to_date}
classes << "missed" if missed_dates.include?(day)