手动设置日期时间(Rails 4)

时间:2015-07-28 19:14:04

标签: ruby-on-rails datetime

所以我试图在更新方法的控制器中手动设置@button_finish_div的percurso_time属性。基本上如果percurso attibute的值在之前更新并且之后为true之前为false,我想将percurso_time设置为当前日期时间。但是,即使我的代码运行没有错误,当我检查rails控制台上的值时,我发现percurso_time是否仍为零。我做错了吗?

def update
  before_percurso = @button_finish_div.percurso

  if @button_finish_div.update(button_finish_div_params)

    after_percurso = @button_finish_div.percurso

    if before_percurso == false && after_percurso == true
      @button_finish_div.percurso_time = DateTime.now
    end
  end
end

1 个答案:

答案 0 :(得分:1)

您只需为其分配值,您需要在分配后保存它 @button_finish_div.save

或者您可以使用update_attributeupdate_column进行更新。