草庐IT

Current_DateTIme

全部标签

ruby-on-rails - 如何在 Rails 3 中为 DATETIME 列保存当前日期/时间?

我有一个名为last_profile_update的DATETIME列。我需要用当前时间更新它。我有以下代码:user=User.newuser.last_profile_update=Date.todayuser.save但它不起作用。 最佳答案 也许是一种简化该代码的方法:user=User.create(:last_profile_update=>Time.now) 关于ruby-on-rails-如何在Rails3中为DATETIME列保存当前日期/时间?,我们在StackOve

ruby - 我想覆盖设计 gem 的 authenticate_user 和 current_user 方法

我想覆盖authenticate_user!和我的应用程序Controller中设计gem的current_user方法你能帮我解决这个问题吗谢谢 最佳答案 你可以像猴子一样修补它:moduleDevisemoduleControllersmoduleHelpersdefauthenticate_user!#dosomestuffendendendend但我会问最终目标是什么,因为Devise已经内置了一些可定制性,覆盖这些方法让我想知道“为什么要使用Devise?” 关于ruby-我想

ruby-on-rails - 如何在 Rails 中将随机 DateTime 四舍五入到小时开始?

基本上我想在去年获得一个随机日期时间:rand(1.year).ago#=>Sun,22Sep201318:37:44UTC+00:00(example)但是我该如何指定或限制在整点时间呢?例如:Sun,22Sep201318:00:00UTC+00:00Sat,02Nov201310:00:00UTC+00:00Fri,12Apr201321:00:00UTC+00:00 最佳答案 我终于找到了我要找的东西。@Stoic的回答很好,但我找到了这个可用的方法(http://api.rubyonrails.org/classes/Da

ruby-on-rails - 我什么时候应该在 ruby​​/rails 中使用 DateTime 与日期、时间字段?

我目前有单独的game_date和game_time字段,由于时区问题,我很难将我的DateTime.now与串联的DateTime进行比较。我应该重新设计我的数据库以仅使用DateTime吗?我单独有一个时间字段,因为时间在某些时间点可以为NULL。通常的做法是什么,以及我应该如何解决以下时区的问题?now=DateTime.now@upcoming_games=[]@past_games=[]games.eachdo|game|game.game_time=DateTime.nowifgame.game_time.nil?dt=DateTime.parse("#{game.game

ruby-on-rails - Rails 中至少有 2 个 DateTime?

如何在Rails中找到2个DateTime对象中的最小值? 最佳答案 最rubyish的方式是使用[a,b].min但你也可以使用C风格(a>b)?b:a希望这对您有所帮助! 关于ruby-on-rails-Rails中至少有2个DateTime?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/5071334/

ruby - Rails current_path helper ?

我正在开发具有以下路由条件的Rails3.2应用程序:scope"(:locale)",locale:/de|en/doresources:categories,only:[:index,:show]get"newest/index",as::newestend我有一个包含以下内容的Controller:classLocaleController我在模板中将它与类似的东西一起使用:=link_toset_locale_path(locale::de,return_to:current_path(locale::de))do=image_tag'de.png',style:'vertic

ruby - DateTime.strptime 我做错了什么?

当我这样做时,我的ruby​​程序说我的日期无效:format="%D/%M/%Y%H:%M:%S:3N"date="21/03/201116:39:11.642"DateTime.strptime(time,format)这个我也试过:format="%D/%M/%Y%H:%M:%S:3"我得到的是这个:ArgumentError:invaliddatefrom/usr/local/lib/ruby/1.9.1/date.rb:1688:in`new_by_frags'from/usr/local/lib/ruby/1.9.1/date.rb:1713:in`strptime'fro

ruby - 你如何让 DateTime.parse 返回你所在时区的时间?

我需要这个require'date'DateTime.parse"Mon,Dec276:30pm"返回EDT时区下午6:30的DateTime,但返回UTC时间。如何获取EST日期时间或将UTC日期时间转换为具有下午6:30值的EDT日期时间? 最佳答案 好的,我要回答我自己的问题require'time'ENV["TZ"]="US/Eastern"Time.parse("Mon,Dec276:30pm").to_datetime=># 关于ruby-你如何让DateTime.parse

ruby-on-rails - 将 ActiveSupport::TimeWithZone 转换为 DateTime

我试图在两个日期之间每隔N天步进一次。我尝试了以下代码,但没有用,因为startDate和endDate是ActiveSupport::TimeWithZone对象,而不是我想的DateTime对象。startDate.step(endDate,step=7){|d|putsd.to_s}min.step(max,step=stepInt){|d|putsd.to_s}如何将TimeWithZone对象转换为DateTime? 最佳答案 我认为更新这个答案可能会有用,因为我最近在搜索这个。实现此转换的最简单方法是使用.to_date

ruby-on-rails - 如何在 rspec 功能测试中访问(设计)current_user?

在设计文档中,他们提供了有关在测试Controller时如何访问current_user的提示:https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29但是,在进行功能测试时呢?我正在尝试测试我的一个Controller的创建方法,并且在该Controller中使用了current_user变量。问题是devise中建议的宏使用了@request变量,并且对于功能规范来说它是nil。什么是解决方法?编辑:这是我目前的规范:feature