草庐IT

current-when

全部标签

ruby - 测试变量是否与多个字符串中的任何一个匹配,而没有长的 if-elsif 链或 case-when

我想在ruby​​中有一种很好的单行方式来表达ifmystr=="abc"or"def"or"ghi"or"xyz"但在我通常查阅的在线引用资料中找不到如何做到这一点...谢谢! 最佳答案 也许你不知道你可以在一个案例上放置多个条件:casemystrwhen"abc","def","ghi","xyz"..end但是对于这个特定的基于字符串的测试,我会使用正则表达式:ifmystr=~/\A(?:abc|def|ghi|xyz)\z/如果您不想构造正则表达式,也不想使用case语句,您可以创建一个对象数组并使用Array#incl

ruby-on-rails - ruby rails : permission denied when using "rails generate controller welcome"

我在RedHat服务器上使用RubyonRails。当我试图生成一个Controller文件时,我得到了这个错误:[ec2-user@ip-172-31-22-128testApp4]$railsgeneratecontrollerwelcome/home/ec2-user/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/server.rb:22:in`initialize':Permissiondenied@rb_sysopen-/tmp/spring/fea371aaf9d69cfa58bd12f69b3f1bf6.pid(Err

ruby-on-rails - 抽取 Assets :precompile gets killed when there is a console session open in production

在我托管在digitalocean上的生产服务器上,如果有帮助的话,Ubuntu12.04,我有RoR4和rake10.1.1。当我部署时,我运行rakeassets:precompile,我注意到一个奇怪的问题,如果我在执行此操作时打开了一个railsconsolesession,我得到以下输出~#rakeassets:precompile~#Killed主要是很烦人,但我希望解决这个问题的原因是在雇用新开发人员时,会有部署/控制台冲突噩梦。谢谢,布莱恩 最佳答案 您的预编译进程可能被终止,因为您的内存不足。您可以通过在另一个ss

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

ruby-on-rails - Rails : how to get the current host, 或用它构建 URL?

这个问题在这里已经有了答案:GettingtheHostnameorIPinRubyonRails(12个答案)关闭8年前。我需要使用主机构建具有不同端口的URL。例如,如果主机是example.com,我需要生成类似http://example.com:8080/的URL我需要它是便携的,所以当我在本地环境中时,它会显示http://localhost:8080/相反。有什么想法吗?

ruby - 冒号(:) appears as forward slash (/) when creating file name

我正在使用日期和时间来标记我正在创建的新文件,但是当我查看该文件时,冒号是一个正斜杠。我正在使用10.7+在Mac上开发这是我使用的代码:File.open("#{time.hour}:00,#{time.month}-#{time.day}-#{time.year}","a")do|mFile|mFile.syswrite("#{pKey}-#{tKey}:\n")mFile.syswrite("Itemsclosed:#{itemsClosed}|Totalitems:#{totalItems}|Percentclosed:%#{pClosed}\n")mFile.syswrite

ruby - 为什么 "case"和 "when > 2"不起作用?

为什么这不起作用?caseARGV.lengthwhen0abort"Error1"when>2abort"Error2"end 最佳答案 这不是有效的ruby​​语法。你需要的是casewhenARGV.length==0abort"Error1"whenARGV.length>2abort"Error2"end当您编写casex时,您需要了解的重要部分是ruby​​获取x,然后将比较应用于您在when中插入的参数或表达式>子句。你说whenx>2的那一行读到ruby​​就像:ifARGV.length==>2当您从case语句中

ruby - 如何在 "case when"中捕获 Errno::ECONNRESET 类?

我的应用程序(Ruby1.9.2)可能会引发不同的异常,包括网络连接中断。我rescueException=>e,然后执行case/when以不同的方式处理它们,但是我的案例中有几个错误直接到else.rescueException=>epe.classcasee.classwhenErrno::ECONNRESETp1whenErrno::ECONNRESET,Errno::ECONNABORTED,Errno::ETIMEDOUTp2elsep3endend打印:Errno::ECONNRESET3 最佳答案 这是因为===运算

ruby-on-rails - 错误 : failed to build gem native extension when installing rails on mac mountian lion os

我最近更新到MountainLion并重新安装了Ruby,但是当我尝试运行测试Rails应用程序时,我收到一条错误消息,指出“我的系统当前未安装Rails”。我按照它说的做,输入sudogeminstallrails并得到:clearedfaster_requirecachesduetonewgeminstall...Successfullyinstalledrails-3.2.71geminstalledInstallingridocumentationforrails-3.2.7...InstallingRDocdocumentationforrails-3.2.7...但是当我检

ruby-on-rails - Kaminari & Rails 分页 - 未定义的方法 `current_page'

我搜索了又搜索,但没有解决我的问题。这是我的Controller:defshow@topic=Topic.find(params[:id])@topic.posts=@topic.posts.page(params[:page]).per(2)#2fordebuggingend这很好用,因为主题View被缩减为两个帖子。但是,当我将其添加到show.html.erb时:我收到这个错误:undefinedmethod`current_page'for# 最佳答案 尝试:defshow@topic=Topic.find(params[: