这个方法:
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
Run Code Online (Sandbox Code Playgroud)
如果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'
Run Code Online (Sandbox Code Playgroud)
但是,如果我更改date = date.to_date为self.date = self.date.to_date,该方法可以正常工作.
这是怎么回事?一般来说,我self什么时候写?
编辑:这与问题无关,但请注意,没有"标题!" 方法.
ruby ×1