How do I use an ActionView::Helper in a Ruby scrip

2019-03-10 20:50发布

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

2条回答
相关推荐>>
2楼-- · 2019-03-10 21:23

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-03-10 21:32
~> 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" 
查看更多
登录 后发表回答