草庐IT

number_of_words

全部标签

ruby-on-rails - time_ago_in_words 和本地化

是否可以将time_ago_in_words与i18n.locale一起使用?如何?谢谢 最佳答案 如果您使用的是Rails>2.2,则该帮助器已经可以识别区域设置。只需从localerepository下载正确的本地化文件即可并将其存储到您的/config/locales路径中。然后设置您的语言环境首选项。 关于ruby-on-rails-time_ago_in_words和本地化,我们在StackOverflow上找到一个类似的问题: https://st

ruby-on-rails - 为什么我不想到处使用 inverse_of?

如这里所述:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.htmlinverse_of似乎告诉Rails缓存内存关联并最小化数据库查询。他们的例子是:classDungeon:dungeonhas_one:evil_wizard,:inverse_of=>:dungeonendclassTrap:trapsend他们立即跟进:for`belongs_to`associations`has_many`inverseassociationsareignored.所以我有几个问题。has_m

ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app

GivenIhavearailsappAndI'musingcucumberAndI'musingcapybaraAndIhaveanactionthatresultsinaredirect_to"http://some.other.domain.com/some_path"WhenItestthisactionThenthein-appportionofthetestworksfineButIseethiserror:Noroutematches"/some_path"with{:method=>:get}(ActionController::RoutingError)所以capyb

ruby-on-rails - rails 3 : validate presence of at least one has many through association item

我有两个模型:Project和ProjectDiscipline:classProject:destroyhas_many:project_disciplines,through::project_disciplinizationsattr_accessible:project_discipline_idsattr_accessible:project_disciplines_attributesaccepts_nested_attributes_for:project_disciplines,:reject_if=>proc{|attributes|attributes['name'

ruby 正则表达式 "contains a word"

在Ruby中,如何编写正则表达式来检查提交的单个单词?假设我有一个接受文本的Web表单。我知道如果我想看看句子--only--是否包含"join"我可以使用ifthe_body=="join"但这只有在整个文本提交都是“加入”时才有效。如何捕捉这样的提交:“我想加入你们的俱乐部?”或者“请加入我”谢谢! 最佳答案 你可以这样做string=~/join/i#/imakesitcaseinsensitive或string.match(/join/i)关于性能评论的一点更新:>>s="iwanttojoinyourclub">>n=50

ruby-on-rails - ruby rails : Converting "SomeWordHere" to "some word here"

我知道你可以这样做:"SomeWordHere".underscore.gsub("_","")得到“这里的一些话”。我认为对于如此简单的事情来说可能有点太多了。是否有更有效的方法(也许是内置方法?)将“SomeWordHere”转换为“此处的某个词”? 最佳答案 方法underscore和humanize是为表、类/包名称等之间的转换而设计的,您最好使用自己的代码进行替换以避免惊喜。查看评论。"SomeWordHere".underscore=>"some_word_here""SomeWordHere".underscore.h

ruby - 如何避免 RSpec 中的 "Useless use of == in void context"?

在RSpec中,如果我有警告并且有x.should==42another_line_of_code然后我得到一个关于的警告warning:uselessuseof==invoidcontext还有什么我可以做的吗关闭警告将其更改为bitbucket=(x.should==42) 最佳答案 使用:x.shouldeq(42)或者:x.shouldbe==42或者移动x.should==42使其成为itblock中的最后一行。对于那些思考但是为什么?的人我完全是Ruby的菜鸟,但这是我的理解:警告来自Ruby,因为像x.should==

ruby-on-rails - 摩卡 : How to add expectation of a method when there are multiple invocations with different parameters

我有一个RailsControllerAction要测试。在那个Action中,一个方法User.can?使用不同的参数多次调用。在其中一个测试用例中,我想确保User.can?('withdraw')被调用。但我不关心User.can的调用?与其他参数。defaction_to_be_tested...@user.can?('withdraw')...@user.can?('deposit')...end我在测试中尝试了以下:User.any_instance.expects(:can?).with('withdraw').at_least_once.returns(true)但是测

ruby-on-rails - rails : Opposite of Hash#to_param

如果我将哈希值转换为查询字符串,我该如何将其再次转换回来?{:filters=>{:colour=>['Red','Blue'],:size=>'Medium'}}.to_param=>"filters[colour][]=Red&filters[colour][]=Blue&filters[size]=Medium"Rails似乎在填充params散列时自动执行此操作,但是否可以直接调用此方法?谢谢。 最佳答案 您正在寻找Rack::Utils.parse_nested_query(query),它将把它转换回Hash。您可以使用

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