Reputation: 171321
In views/products/list.html.erb
I use:
<%= product.power.power_in_kw.to_kw if ... %>
to_kw
is defined in lib/my_extensions.rb
along with other methods:
class Symbol
def pluralize
to_s.pluralize.to_sym
end
end
class BigDecimal
def to_kw
number_to_currency(self, :unit => "kw", :format => "%n%u", :precision => 1)
end
end
class Float
def to_dollar
number_to_currency(self)
end
end
config/environment.rb
has the following line at the end:
require 'my_extensions'
However, I got the following error:
undefined method `to_kw' for #<BigDecimal:2704620,'0.555E2',8(8)>
What am I missing ?
Upvotes: 1
Views: 495
Reputation: 1855
You should include ActionView::Helpers::NumberHelper
in your BigDecimal
and Float
:
class BigDecimal
include ActionView::Helpers::NumberHelper
def to_kw
number_to_currency(self, :unit => "kw", :format => "%n%u", :precision => 1)
end
end
class Float
include ActionView::Helpers::NumberHelper
def to_dollar
number_to_currency(self)
end
end
I think the error undefined method to_kw
is caused by the undefined method number_to_currency
.
Upvotes: 1
Reputation: 85784
I know it's been hours since you submitted this, but these functions might work once you restart your app. Items in lib
generally are not reloaded automatically like those in app
, so changes made will not be reflected in the application until performing a full restart.
Just throwing it out there :)
I'm also going to point out that, once you have these methods up and running, they probably will not work immediately. This is because your views are defined in the context of all of the Rails view helpers, like ActionView::Helpers::NumberHelper
, which defines number_to_currency
. Your extension in lib
, however, is not defined in such a context, and therefore cannot access those helpers.
ActionView::Helpers::NumberHelper.number_to_currency
might be more likely to work as expected.
Upvotes: 2