草庐IT

ticket_generate_date

全部标签

ruby - Date::today 未定义?

为什么只有Date类的某些方法没有显式加载:require'date'线?例如:irb(main):002:0>Date.todayNoMethodError:undefinedmethod`today'forDate:Classfrom(irb):2from/Users/mwlang/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in`'然后……irb(main):003:0>require'date'=>true导致...irb(main):004:0>Date.today=>#http://ruby-doc.org/stdlib-2.0/libdoc/

ruby - 转储 JSON 对象的首选方法是什么? to_json、JSON.generate 还是 JSON.dump?

我需要将哈希对象转储到JSON,我想知道这三个中的哪一个,to_json、JSON.generate或JSON.dump,是执行此操作的首选方法。我已经测试了这些方法的结果,它们是相同的:>{a:1,b:2}.to_json=>"{\"a\":1,\"b\":2}">JSON.generate({a:1,b:2})=>"{\"a\":1,\"b\":2}">JSON.dump({a:1,b:2})=>"{\"a\":1,\"b\":2}" 最佳答案 来自docs:JSON.generateonlyallowsobjectsorarr

ruby-on-rails - Rails : Date. 今天 - 1.day

使用rails控制台,我只是被这个咬住了:假设今天是12月11日。Date.today-1.day#December10(nospaces)Date.today-1.day#December10(aspaceonbothsidesoftheminussign)Date.today-1.day#December11whaaaat?Date.today-5.days#Stilldecember11!有人能解释一下这是怎么回事吗?我有点担心这在代码中很容易被遗漏。关于如何对此进行编码还有其他建议吗? 最佳答案 您看到的差异是由ruby​​

ruby-on-rails - rails 3 : how to generate custom error message from failed validation

我正在使用validates:feed_id,presence:true,uniqueness:true我应该如何生成自定义错误消息以指定用户已经订阅了此提要(feed_id)字段重复我知道我可以只做validate_uniqueness_of但它会不必要地使代码困惑。如果唯一性验证失败,我该如何传递特定的错误消息?? 最佳答案 使用键message和期望的消息作为值而不是true放置一个散列:validates:feed_id,presence:true,uniqueness:{message:"alreadysubscribed

ruby - 导轨 : seeding database data and date formats

我正在尝试使用db/seed.rb文件和rakedb:seed命令将一些数据播种到我正在开发的Rails3应用程序中。我播种的数据涉及一个名为Meeting的数据库表,该表具有三列:字符串标题、日期时间开始日期、日期时间结束日期。我尝试插入的日期时间采用“mm/dd/yyyyhh:mm”格式——例如。“01/02/200313:23”=2003年1月2日下午1:23。但是,DateTime.parse()始终会出现“无效日期”错误——因为它试图以“dd/mm/yyyy”格式解析日期。根据我的谷歌搜索,我被引导相信在解析DateTime对象时,编译器会查看传入的字符串并进行一些仓促的模式

ruby-on-rails - 错误 "' Validate_default_type !': An option' s default must match its type (ArgumentError)"when running Ruby on Rails generate on Windows

我正在关注thistutorial并且刚刚开始。我已经使用geminstallrails安装了RubyonRails,并使用railsnewblog创建了一个博客。教程现在说我需要运行railsgeneratecontrollerWelcomeindex,但是当我这样做时,我得到了这个错误:C:/Ruby22/lib/ruby/gems/2.2.0/gems/thor-0.19.2/lib/thor/parser/option.rb:130:in`validate_default_type!':Anoption'sdefaultmustmatchitstype.(ArgumentErr

ruby-on-rails - rails 3 : Generate unique codes (coupons)

生成唯一代码以用作优惠券代码的最佳方法是什么?谢谢。 最佳答案 在Ruby的标准库中有SecureRandom用于此:SecureRandom.hex(3)输出的长度是输入指定长度的两倍。 关于ruby-on-rails-rails3:Generateuniquecodes(coupons),我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4558250/

ruby - 在 Ruby 中,为什么等于 nil ("Date.new == nil") 返回 nil?

今天写一些rspec时,我遇到了一些意外的行为,将日期(和时间)实例与nil进行比较。这是一个使用原始ruby​​的示例(没有Rails或其他库):user@MacBook-Work~$ruby-vruby1.8.7(2008-08-11patchlevel72)[universal-darwin10.0]user@MacBook-Work~$irb>>1==nil=>false>>"string"==nil=>false>>:sym==nil=>false>>false==nil=>false>>[]==nil=>false>>{}==nil=>false>>Proc.new{}==

ruby - 轨道 3 :how to generate models for existing database tables

我已经将我的database.yml配置为指向我现有的mysql数据库我如何从中生成模型?railsgeneratemodelexisting_table_name只给出一个空模型.. 最佳答案 你可以试试Rmre.它可以为现有模式创建模型,并尝试根据外键信息创建所有关系。 关于ruby-轨道3:howtogeneratemodelsforexistingdatabasetables,我们在StackOverflow上找到一个类似的问题: https://st

ruby-on-rails - Date 与 ActiveSupport::TimeWithZone 的比较失败

我的Waiver模型上有一个age方法,如下所示:defage(date=nil)ifdate.nil?date=Date.todayendage=0unlessdate_of_birth.nil?age=date.year-date_of_birth.yearage-=1ifdate然后我有一个看起来像这样的规范:it"calculatestheproperage"dowaiver=FactoryGirl.create(:waiver,date_of_birth:12.years.ago)waiver.age.should==12end当我运行这个规范时,我得到Comparisono