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

前端 未结 2 569
半阙折子戏
半阙折子戏 2021-02-02 10:59

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

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 11:15

    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.

提交回复
热议问题