Checking day against date in Rails 3 using simple_form -
what's best way check date against day in rails?
i have register registers take place on days, , want restrict selectable dates match days i.e. if register set monday 15:00, date selection should provide dates monday i.e. 1st april 2013, 8th april 2013, 15th april 2013 etc.
here screenshot of current register looks like:
my form uses simple_form
gem, , has following simple input:
<%= f.input :date %>
edit: here model, requested cthulhu
class register < activerecord::base has_many :student_registers has_many :students, :through => :student_registers belongs_to :event attr_accessible :date, :student_ids, :module_class_id, :event_id end
date class has plenty of useful methods, like
>> d = date.today => fri, 05 apr 2013 >> d.monday? => false >> d.friday? => true >> d.wday => 5
it should enough write validation method:
class register < activerecord::base validates_each :date |record, attr, value| record.errors.add(attr, "must #{event.day}") unless value.wday == date.parse(event.day).wday # assume 'event.day' holds weekday string, e.g. 'monday' end end
Comments
Post a Comment