Not able to use "number_with_precision" in ApplicationHelper's method in Rails 5.0
Asked Answered
R

3

6

I am using "number_with_precision" method in Rails View, it is working fine there, for example:

In Rails View: (works fine)

<%= number_with_precision(job.company.rating, :precision => 2) %>

But when I try to do the same in ApplicationHelper's method then it gives me the following error:

undefined method `number_with_precision' rails

Here is what I have tried:

In Rails ApplicationHelper: (gives error)

module ApplicationHelper

  def employer_overall_rating(overall_rating)
      @overall_rating = number_with_precision(overall_rating, :precision => 2)
  end

end

How can I use the same in ApplicationHelper?

Ricoriki answered 5/10, 2016 at 12:48 Comment(4)
include ActionView::Helpers::NumberHelper in your application_helper.rb ?Postrider
Try helper.number_with_precision(overall_rating, :precision => 2)Opinionative
@孙悟空 i wish if you have posted this as an Answer, so that i could accept this, anyways thanks alot :)Ricoriki
@孙悟空 Please post the answerOpinionative
E
14

include ActionView::Helpers::NumberHelper in your application_helper.rb

Extenuation answered 5/10, 2016 at 13:33 Comment(0)
S
8

If you don't want to clutter the namespace with number helpers, one way of doing that is calling the helper through ActionController::Base.helpers.

def employer_overall_rating(overall_rating)
  @overall_rating = ActionController::Base.helpers.number_with_precision(overall_rating, :precision => 2)
end

Naturally that would be simplified by:

def helpers
  ActionController::Base.helpers
end

Then you could do just helpers.number_with_precision

Another way is to include ActionView::Helpers::NumberHelper

Structuralism answered 5/10, 2016 at 13:29 Comment(0)
U
2

Try Following

module ApplicationHelper
include ActionView::Helpers::NumberHelper
    def employer_overall_rating(overall_rating)
      @overall_rating = number_with_precision(overall_rating, :precision => 2)
    end
end
Unbodied answered 5/10, 2016 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.