gpt4 book ai didi

ruby-on-rails - 如何在 Controller 中使用新的 ActiveSupport::Testing::TimeHelpers

转载 作者:行者123 更新时间:2023-12-03 15:59:14 28 4
gpt4 key购买 nike

编辑以使其更清晰

就像标题告诉如何使用真棒ActiveSupport::Testing::TimeHelpers#travel_to我的 Controller 内部的方法。就像在测试中一样,我想实现这样的目标:

SomethingConroller < ApplicationController
def index
travel_to some_date do
# some stuff that it depends on the current_date
end
end
end

尝试包含模块:
include ActiveSupport::Testing::TimeHelpers

但我得到了:
uninitialized constant ActiveSupport::Testing

希望旅行的日期将应用于 view , view_helpers , controller_action

最佳答案

如果必须,请添加 require 'active_support/testing/time_helpers'在文件的顶部。

虽然我不知道你在用那个代码做什么。试试这个:

SomethingConroller < ApplicationController
def index
@instance = SomeModel.find_by_date(12.days.ago)
end
end

关于ruby-on-rails - 如何在 Controller 中使用新的 ActiveSupport::Testing::TimeHelpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30645515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com