Formatting text in the model with Ruby on Rails -
how can make mvc?
class product < activerecord::base validates :price, :presence => true def to_s "#{name} @ #{number_to_currency(price)}" end end
i need format price currency, can't use number_to_currency, because in model. pass view this, doesn't feel clean.
a solution define producthelper
module in app/helpers
implements method want, product_name_with_price
:
module producthelper def product_name_with_price(product) "#{product.name} @ #{number_to_currency(product.price)}" end end
and in view
<%= product_name_with_price(@product) %>
Comments
Post a Comment