开发者

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

开发者 https://www.devze.com 2023-03-23 15:51 出处:网络
I am looking to use ActionView::Helpers::NumberHelper from a Ruby script. What all do I need to requir开发者_运维知识库e etc.?~> irb

I am looking to use ActionView::Helpers::NumberHelper from a Ruby script. What all do I need to requir开发者_运维知识库e etc.?


~> 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" 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消