这种方法:
def format_stations_and_date from_station.titelize! if from_station.respond_to?(:titleize!) to_station.titleize! if to_station.respond_to?(:titleize!) if date.respond_to?(:to_date) date = date.to_date end end
当日期为nil时,此错误失败:
NoMethodError (You have a nil object when you didn't expect it! The error occurred while evaluating nil.to_date): app/models/schedule.rb:87:in `format_stations_and_date' app/controllers/schedules_controller.rb:15:in `show'
但是,如果我将date = date.to_date更改为self.date = self.date.to_date,则该方法正常工作.
这是怎么回事?一般来说,我什么时候要写自我?