我需要将日期存储在变量中:created_date + 1 年。变量名为decided_at,是date
类型。我在 Controller 中编写了以下代码:
def create
@post = current_user.posts.new(post_params)
@post.statut = 1
@post.decided_at = @post.created_at + 1.year // here is the problem
if @post.save
redirect_to @post
else
render 'new'
end
end
它返回:nil:NilClass 的未定义方法“+”
如何修复这个问题?
最佳答案
您正在 Controller 中构建一个新帖子 - 但未保存它,因此它的 created_at
尚未设置。
我不确定您为什么要为其添加一年,但我建议您在尝试更新之前保存它,或者只是将其设置为当前时间 + 1 年。
关于ruby-on-rails - Rail 4 中将日期增加一年。未定义方法 `+' for nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22352560/