How to use the “number_to_currency” helper method in the model rather than view?

后端 未结 11 1534
有刺的猬
有刺的猬 2021-01-30 03:59

I would like to use to_dollar method in my model like this:

module JobsHelper      
  def to_dollar(amount)
    if amount < 0
      number_to_cur         


        
11条回答
  •  佛祖请我去吃肉
    2021-01-30 04:11

    It is not a good practice but it works for me!

    to import include ActionView::Helpers::NumberHelper in the controller. For example:

    class ProveedorController < ApplicationController
        include ActionView::Helpers::NumberHelper
        # layout 'example'
    
        # GET /proveedores/filtro
        # GET /proveedores/filtro.json
        def filtro
            @proveedores = Proveedor.all
    
            respond_to do |format|
                format.html # filtro.html.erb
                format.json { render json: @proveedores }
            end
        end
    
        def valuacion_cartera
            @total_valuacion = 0
            facturas.each { |fac|
                @total_valuacion = @total_valuacion + fac.SumaDeImporte
            }
    
            @total = number_to_currency(@total_valuacion, :unit => "$ ")
    
            p '*'*80
            p @total_valuacion
        end
    end
    

    Hope it helps you!

提交回复
热议问题