html - 如何 == 为 CSS Helper 中的模型方法类?

标签 html css ruby-on-rails ruby helper

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

Pages#home 中的名称错误

undefined local variable or method 'current_user'

CSS

.future { background-color: #FFF; }
.missed { background-color: red; }

我用 railscasts tutorial 建立了日历.

最佳答案

将这一行添加到您的 Calendar 类中:

delegate :current_user, to: :view

由于 SessionsHelper 自动包含在 View 中,它应该能够将您的 view 变量传递给您的 Calendar 类。

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)

关于html - 如何 == 为 CSS Helper 中的模型方法类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638430/

相关文章:

ruby-on-rails - rails 3.2 + OSX : Cannot install rmagick gem

javascript - HTML 5 动态加载视频导致 iPad 黑屏

javascript - PHP解释器将外部js和css转成html内嵌代码

css - 为什么这个文本区域在 Safari 中看起来更小?

html - 动态网站 CSS 翻转 - DIV 内的 100% DIV 高度与 CSS display=table-cell

ruby-on-rails - 注释掉 haml 表中的项目

mysql - Rails 开发 - 无法连接到 'localhost' (10061) 上的 MySQL 服务器

jquery - 如何让此视频播放器在网络上使用 html5 ogv 和 mp4?

javascript - 如何根据 geoJson 功能为每个集群着色不同?

javascript - JS不读取第一个ID,只读取第二个ID