How do I use an ActionView::Helper in a Ruby script, outside of Rails?

大憨熊 提交于 2019-12-20 09:49:30

问题


I am looking to use ActionView::Helpers::NumberHelper from a Ruby script. What all do I need to require etc.?


回答1:


~> irb
ruby-1.9.2-p180 :001 > require 'action_view'
 => true 
ruby-1.9.2-p180 :002 > ActionView::Base.new.number_to_currency 43
 => "$43.00" 



回答2:


As of Rails 3.2.13, you can do the following:

class MyClass
  include ActionView::Helpers::NumberHelper

  def my_method
    ...
    number_with_precision(number, precision: 2)
    ...
  end
end

You might need to require 'action_view' too.

Edit: This answer is still valid in Rails 4.2.3.



来源:https://stackoverflow.com/questions/6853015/how-do-i-use-an-actionviewhelper-in-a-ruby-script-outside-of-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!