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

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -