AnthonyGalli.com
AnthonyGalli.com

Reputation: 2866

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

NameError in Pages#home

undefined local variable or method 'current_user'

css

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

I built the calendar off of a railscasts tutorial.

Upvotes: 1

Views: 41

Answers (1)

Aleksey Shein
Aleksey Shein

Reputation: 7482

Add this line to your Calendar class:

delegate :current_user, to: :view

Since SessionsHelper is get automatically included into view, it should be able your view variable, that is getting passed to your Calendar class.

Thank you that removed the error, but for some reason the days that I marked as date_missed didn't turn red

I guess current_user.missed_dates.group_by {|i| i.date_missed.to_date} is an array of missed dates, so, in order to check if your day is in that array, use include?:

missed_dates = current_user.missed_dates.group_by {|i| i.date_missed.to_date}
classes << "missed" if missed_dates.include?(day)

Upvotes: 1

Related Questions