我有一个来自表的实例变量ride ,它有一个列ride.pickup_at
如果我做<%= ride.pickup_at.class.name %>我得到
时间
所以Strftime应该可用。
现在,如果我做<%= ride.pickup_at.strftime('%x') %>我收到错误
nil的未定义方法`strftime':NilClass
我需要做些什么才能使strftime可用?
I am have a instance variable ride that comes from a table and it has a column ride.pickup_at
If I do <%= ride.pickup_at.class.name %> I get
time
So Strftime should be available.
Now, if I do <%= ride.pickup_at.strftime('%x') %> I get an error
undefined method `strftime' for nil:NilClass
What do I have to do to make strftime available?
最满意答案
你可以try使用nil判断: <%= ride.pickup_at.try(:strftime, '%x') %> 。或者我认为如果ride.pickup_at nil你应该弄清楚该做什么
you can use try to do the nil judge: <%= ride.pickup_at.try(:strftime, '%x') %> .Or I think you should figure out what to do if ride.pickup_at is nil
更多推荐
发布评论