【发布时间】:2009-08-09 19:31:10
【问题描述】:
这个方法:
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
当date 为 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,则该方法可以正常工作。
发生了什么事?一般来说,我什么时候必须写self?
编辑:与问题无关,但请注意没有“titleize!”方法。
【问题讨论】:
标签: ruby