草庐IT

CURRENT_AS_PATHNAME

全部标签

ruby - RSpec - as_null_object?

我不清楚as_null_object的目的是什么是什么情况下你会使用它?根据关于Relish的文档:Usetheas_null_objectmethodtoignoreanymessagesthataren'texplicitlysetasstubsormessageexpectations.没有向我注册。 最佳答案 当您不关心对象的行为或交互,并且不想显式地stub所有需要的东西时,使用空对象。使用空对象上的任何参数调用任何方法都不会导致NoMethodError或ArgumentError。另请参阅NullObjectPatte

ruby-on-rails - rails : How to print a decimal as a percent?

有没有办法将小数打印为百分比,所以只有句点后的两位数?我的小数点总是在1和0之间,所以我想从第三个字符开始调用number.round(2)是可行的,但我找不到它的语法任何地方。为了澄清,我希望将数字存储为完整的小数,但打印为百分比。 最佳答案 您可能想要使用number_to_percentage方法。来自documentation,这里有一些如何使用它的例子:number_to_percentage(100)#=>100.000%number_to_percentage("98")#=>98.000%number_to_perc

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 - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec

我有以下测试:let(:client){Descat::Client.new}describe'poblacio'doit'shouldsetformatcorrectly'doclient.poblacio('v1','json','dades')expect(client.instance_variable_get(:format)).toeq('json')endend我有以下正在测试的代码:moduleDescatclassClientBASE_URL='http://api.idescat.cat/'definitialize(attributes={})attributes

ruby - "(...) interpreted as grouped expression"是什么意思?

我在Atom中使用Rubylinter,对于某些行,它会发出以下警告:(...)interpretedasgroupedexpression获取此警告的行示例如下:elsifnot(params[:vacancy].nil?orparams[:vacancy]['company_id'].nil?orparams[:vacancy]['company_id']=="0")应该如何改进该行以使警告消失? 最佳答案 警告是(...)interpretedasgroupedexpression它的意思就是它所说的:在Ruby中,圆括号可以

ruby-on-rails - 如何在 Rails 中使用 gem 'acts-as-taggable-on' 时获取所有标签的列表(不是计数)

我在我的模型中设置了acts-as-taggable-ongem,如下所示:acts_as_taggable_on:deshanatags它使用上下文deshanatags。现在我需要在上下文中以下列格式获取所有标签的列表(不仅仅是为一个项目分配的标签。我需要所有标签):[{"id":"856","name":"House"},{"id":"1035","name":"DesperateHousewives"}]我该怎么做?我尝试遵循许多教程,但遇到了死胡同,因为它们中的大多数都是为Rails3编写的。Rails对模型进行了一些更改,例如删除了attr_accessor,这让我很难理解

arrays - 如何通过 & :key as an argument to map instead of a block with ruby?

我写了这段代码:my.objects.map{|object|object.key}我的rubocop说:Pass&:keyasanargumenttomapinsteadofablock.有没有捷径可以做同样的事情? 最佳答案 Pass&:keyasanargumenttomapinsteadofablock意思是:my.objects.map(&:key) 关于arrays-如何通过&:keyasanargumenttomapinsteadofablockwithruby?,我们在S

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-on-rails - 序数 : '1' as '1st' , '2' 为 '2nd' 等的 Ruby 格式

ruby或rails中是否有任何东西可以处理序数的格式:'1'为'1st','2'为'2nd',等等? 最佳答案 看起来你正在寻找序号:TheRubyonRailsframeworkischockfullofinterestinglittlenuggets.Ordinalizeisanumberextensionthatreturnsthecorrespondingordinalnumberasastring.Forinstance,1.ordinalizereturns“1st”and22.ordinalizereturn“22n