2011-02-28 38 views
18
irb(main):001:0> require 'active_support' 
=> true 
irb(main):002:0> require 'active_support/inflector/inflections' 
=> true 
irb(main):003:0> ActiveSupport::Inflector.pluralize('test') 
=> "test" 
irb(main):004:0> ActiveSupport::Inflector.singularize('tests') 
=> "tests" 
irb(main):005:0> ActiveSupport::Inflector.titleize('hat simulator') 
=> "Hat Simulator" 
<ort::Inflector.tableize("america's number one hat simulator") 
=> "america's number one hat simulator" 

嗯,基本上,這就是問題所在。令我困惑的是,像titleize這樣的方法似乎可以正常工作,但是tableizepluralizesingularize沒有。無法使用ActiveSupport :: Inflector獲得複數/單數化(在irb中)

我忘了需要什麼嗎?

(在一個單獨的說明,我注意到this page提供像"post".pluralize,這時候我試過了,導致NoMethodError: undefined method 'pluralize' for "post":String例子。但也許這東西救了另一個問題。)

回答

33
require 'active_support/inflector' 
ActiveSupport::Inflector.pluralize('test') 
#=> "tests" 

對於字符串

require 'active_support/core_ext/string' 
"test".pluralize 
#=> "tests" 

至極居然會調用ActiveSupport::Inflector.pluralize

def pluralize 
    ActiveSupport::Inflector.pluralize(self) 
end 
+0

這對我有用,但我很好奇,如果你的控制器中包含助手是特定代碼異味的標誌?我覺得我不應該這樣做...... – 2014-09-06 22:49:47