ruby-on-rails - Rail 4 中将日期增加一年。未定义方法 `+' for nil :NilClass

标签 ruby-on-rails time increment

我需要将日期存储在变量中: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/

相关文章:

mysql - 在日期时间mysql中转换一年中的 float 日期

mysql - 如何在 SELECT 语句中增加一行?

scheme - 方案编程语言中的自增和自减运算符

ruby-on-rails - 如何通过 Nginx 服务器 + Thin on Rails 运行集成测试

ruby-on-rails - 是否可以跳过 Rails 4 中的 prepend_before_action 并设计?

c - 编译所需的 Netbeans C 项目配置

android - 删除保存的 ListView 中的所有内容,并在每次更新时递增 1

ruby-on-rails - 来自浏览器的重复请求

ruby-on-rails - 使用 Paperclip/Fog 将文件保存到 RackSpace CloudFiles 时,连接到奇数 IP 地址时会发生超时

c++ - C/C++ 中线程安全的时区特定时间显示