草庐IT

compile-time-constant

全部标签

ruby-on-rails - TimeWithZone & Time.zone.now 集成测试失败

在Controller方法中,当向用户发送激活电子邮件时,我将用户变量activation_sent_at设置为等于Time.zone.now。在开发服务器上,这似乎可行(尽管我的应用程序中的时间表达式比我计算机的本地时间晚2小时)。我想包括一个集成测试,测试activation_sent_at是否确实设置正确。所以我包括了这一行:assert_equal@user.activation_sent_at,Time.zone.now但是,这会产生错误:NovisibledifferenceintheActiveSupport::TimeWithZone#inspectoutput.You

ruby-on-rails - 计算两个 Time 对象之间的时间差

假设我从我的用户模型中创建了两个时间对象:created=u.created_atupdated=u.updated_at如何计算两个时间对象之间的小时差?hours=created-updated我想将它包装在一个方法中并扩展Time类。我很难相信我需要扩展它,但我似乎无法找到一种本地方法来处理使用不同时间单位计算耗时。 最佳答案 这应该有效:hours=((created-updated)/1.hour).round相关问题:RailsTimedifferenceinhours 关于

ruby-on-rails - 使用 Rails 中的命名空间类防止 "warning: toplevel constant B referenced by A::B"

在自定义目录(例如:app/presenters/)中存储文件时,如何确保加载命名空间类?例如,如果您有:app/models/mega_menu.rbapp/presenters/catalog_presenter.rbapp/presenters/mega_menu/catalog_presenter.rbRails无法加载MegaMenu::CatalogPresenter:CatalogPresenter.new=>#MegaMenu::CatalogPresenter.new(irb):3:warning:toplevelconstantCatalogPresenterref

ruby - 在 Ruby 中从 X.times 返回数组的简洁方法

我经常想对数组执行某项操作X次,然后返回该数字以外的结果。我通常写的代码如下:defother_participantsoutput=[]NUMBER_COMPARED.timesdooutput有没有更简洁的方法来做到这一点? 最佳答案 听起来您可以使用map/collect(它们是Enumerable的同义词)。它返回一个数组,其内容是通过map/collect每次迭代的返回值。defother_participantsNUMBER_COMPARED.times.collectdoParticipant.new(all_frie

ruby-on-rails - 如何在 Rails 之外使用 Rails DateHelper 方法 time_ago_in_words?

因此,我下载并安装了ActiveHelpergem,但我仍然无法弄清楚如何在普通Ruby代码中使用ActionView的DateHelper方法,例如time_ago_in_words。这不包括在ActiveHelper中吗?是否可以在Rails之外使用这些方法?http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words 最佳答案 试试这个:require'action_view'require'ac

ruby-on-rails - rails : Ensure only one boolean field is set to true at a time

我有一个Logo模型,它的字段名称为:字符串,默认为bool值。我希望true值是唯一的,以便一次只能将数据库中的一项设置为true。如何在我的Controller中设置更新和新操作以将Logo的所有其余值设置为false?假设我有以下设置在我的数据库中模特标志名称:字符串|默认值:bool值|项目1|是的|项目2|假|第3项|假|如果我将Item2默认值更改为true,我希望它遍历所有Logo并将其余Logo设置为false,因此一次只有一个为true,所以它看起来像这样。名称:字符串|默认值:bool值|项目1|假|项目2|是的|第3项|假|提前感谢您的帮助。

ruby-on-rails - 为什么我使用 Rails 和 grape 得到 "Unable to autoload constant"?

我想为Android应用做一个API。搜索时,我找到了{grape}.我正在关注thistutorial,但我在启动Rails服务器时遇到问题:=>BootingWEBrick=>Rails4.0.2applicationstartingindevelopmentonhttp://0.0.0.0:80=>Run`railsserver-h`formorestartupoptions=>Ctrl-CtoshutdownserverExitingC:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.2/

ruby-on-rails - 无方法错误 : undefined method `safe_constantize'

我有一个简单的模型:classPost它有一个名为type的INT列当我创建记录时:Post.create(:type=>1)我得到:NoMethodError:undefinedmethod`safe_constantize'for1:Fixnum我做错了什么? 最佳答案 就像这样:在Fixnum上没有名为safe_constantize的方法。type用于SingleTableInheritance.您通常会在应用程序中将已知模型的字符串表示形式作为type值。参见railsguides

ruby gem : Uninitialized constant FactoryBot

使用Rubygem并尝试在RSpec中使用FactoryBot。我在support/factory_bot.rb中有这个:RSpec.configuredo|config|config.includeFactoryBot::Syntax::Methodsconfig.before(:suite)doFactoryBot.find_definitionsendend在spec_helper.rb中:require'support/factory_bot'当我尝试运行specrake任务时,出现此错误:support/factory_bot.rb:2:in`blockin':uniniti

ruby-on-rails - 如何修复 Rake 任务中的 "uninitialized constant"

我在做的时候遇到了问题:namespace:xaarondotask:get_rolesdoroles=Xaaron::Role.allputsrolesendtask:get_role,[:name]do|t,args|role=Xaaron::Role.find(args[:name].parameterize)putsroleendend第一个任务可以正常工作。我什至可以添加binding.pry并运行Xaaron::Role并获取有关Roles的信息。但是第二个任务失败了:NameError:uninitializedconstantXaaron::Role我在我的主应用程序中