我知道這看起來很愚蠢,但我想在我設置的一個rake任務中調用一些Rails的文本助手。 (認爲像複數和循環方法:http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)在rake任務中使用pluralize方法
你會如何去使這些可用的耙任務,或者是不容易的可能嗎?
我知道這看起來很愚蠢,但我想在我設置的一個rake任務中調用一些Rails的文本助手。 (認爲像複數和循環方法:http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html)在rake任務中使用pluralize方法
你會如何去使這些可用的耙任務,或者是不容易的可能嗎?
這是相當凌亂在你的rake任務延長的ActionView ::幫手 - 這主要包括在耙所有輔助方法任務。
ActionController::Base.helpers.pluralize(5, 'dog')
如果你沒有一個計數,你只是想一個單詞變複數:
ActiveSupport::Inflector.pluralize('dog')
添加的ActiveSupport和ActionPack的作爲依賴可能是重手簡單地有一個文本助手的便利 - 除非你的Rake文件已加載Rails的 - 但這裏有一個例子Rake文件:
require 'rubygems'
require 'activesupport'
require 'actionpack'
require 'action_view/helpers'
extend ActionView::Helpers
task :plural do
puts "I have #{pluralize(5, "dog")}"
puts "You have #{pluralize(1, "dog")}"
end
require 'active_support/inflections'
require 'action_view/helpers'
extend ActionView::Helpers
task :plural do
puts "I have #{pluralize(5, "dog")}"
puts "You have #{pluralize(1, "dog")}"
end
如果你只是想串的多元化的版本,你可以調用它的pluralize
的情況下直接在你的rake任務加載額外的東西:
"dog".pluralize
然後,您可以只用像這樣替換以複數幫手:
word = "dog"
if count == 1
plural = "1 #{word}"
else
plural = "#{count} #{word}".pluralize
end
puts plural
非常感謝你:) – 2009-12-31 01:15:02