Rails 5使用link_to更新日期

问题描述 投票:0回答:1

我有Contract模型与accept_date属性

我想通过accept_date使用自定义方法更新link_to,该方法也可以改变其AASM状态。通过将视图中的参数传递给link_to put或者作为link_to get在控制器中执行它是否更好?

view

// put
= link_to 'Accept', custom_method(@contract, contract: {accept_date: Time.now}), method: :put
// get
= link_to 'Accept', custom_method(@contract)

controller

def custom_method
 @contract.aasm_accept!
 @contract.update(contract_params) # if put
 @contract.update_attributes(accept_date: Time.now) # if get

此外,Time.nowDate.today和其他助手之间有什么区别来获得当前时间,他们是否依赖于t.date t.timet.datetime?我可以使用Date.today作为t.time属性吗?

我使用上面的方法,数据库显示提交,但没有存储。

ruby-on-rails ruby-on-rails-5
1个回答
0
投票

您可以在更新状态后直接添加方法,如下所示,所以需要在控制器的方法中写入,使用PUT而不是GET,因为您正在更新某些东西

your_model.rb

event :aasm_accept do
  transitions from: :nothing, to: :everything, after: proc { |*_args| update_date_value }
end


def update_date_value
 # your business logic
end

.current和.now的区别是.now使用服务器的时区,而.current使用Rails环境设置的。如果未设置,则.current将与.now相同。

Date.today将在没有时区的情况下为您提供今天的日期。 Time.current将为您提供当前在rails应用程序中配置的时区的时间。

© www.soinside.com 2019 - 2024. All rights reserved.