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

2

23

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

Piscatelli answered 28/7, 2011 at 0:53 Comment(0)
R
37
~> 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" 
Rahman answered 28/7, 2011 at 0:58 Comment(0)
M
22

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.

Monatomic answered 15/5, 2013 at 13:28 Comment(2)
require 'action_view' is what I was missing. Thank you.Slipover
This was helpful. I think you can skip require 'action_view' if you're running the script via 'rails runner'Regression

© 2022 - 2024 — McMap. All rights reserved.