As previously stated, those are view helpers. Models really shouldn’t
have
any formatting data in them at all… your view layer should do that for
you.
But explain why you want to do number_to_currency in the model (remember
this renders a string) and there may be a way to solve your problem. You can get number_to_currency working in the model – I’ve done it before
but
it’s not a good approach.
As previously stated, those are view helpers. Models really shouldn’t
have
any formatting data in them at all… your view layer should do that for
you.
But explain why you want to do number_to_currency in the model (remember
this renders a string) and there may be a way to solve your problem. You can get number_to_currency working in the model – I’ve done it before
but
it’s not a good approach.
Thanks - I’m fairly new to rails, so maybe i’m going about this in the
wrong way.
I have a Product model with min_price and max_price properties.
In many places throughout the site, I need to display a price range. I
had this in my application_helper:
def get_price_range(min,max)
if min == max
price_range_text = number_to_currency(max)
else
price_range_text = number_to_currency(min)+ ’ - ’ +
number_to_currency(max)
end
end
But I thought it would be nice to make this an accesible value on my
Product model, so in my view I could just say <%=product.price_range%>
as opposed to <%=get_price_range(product.min_price,product.max_price)%>
So I tried adding this to my Product Model:
def price_range
if self.min_price == self.max_price
price_range_text = number_to_currency(self.max_price)
else
price_range_text = number_to_currency(self.min_price)+ ’ - ’ +
number_to_currency(self.max_price)
end
end
But apparently I shouldn’t be doing this?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.