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

后端 未结 11 1515
有刺的猬
有刺的猬 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:07

    Piggybacking off of @fguillen's response, I wanted to override the number_to_currency method in my ApplicationHelper module so that if the value was 0 or blank that it would output a dash instead.

    Here's my code in case you guys would find something like this useful:

    module ApplicationHelper
      def number_to_currency(value)
        if value == 0 or value.blank?
          raw "&ndash;"
        else
          ActionController::Base.helpers.number_to_currency(value)
        end
      end
    end
    
    0 讨论(0)
  • 2021-01-30 04:08

    You can just include ActiveSupport::NumberHelper module, if you don't need additional features defined by ActionView.

    https://github.com/rails/rails/blob/44260581bec06e4ce05f3dd838c8b4736fc7eb1d/actionview/lib/action_view/helpers/number_helper.rb#L383

    0 讨论(0)
  • 2021-01-30 04:10

    I know this thread is very old, but someone can look for solution for this problem in Rails 4+. Developers added ActiveSupport::NumberHelper, which can be used without accessing view related modules/classes using:

    ActiveSupport::NumberHelper.number_to_currency(amount, precision: 0)
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-30 04:12

    You can use view_context.number_to_currency directly from you controller or model.

    0 讨论(0)
  • 2021-01-30 04:17

    Really surprised not one person has talked about using a Decorator. Their purpose is to solve the issue you are facing, and more.

    https://github.com/drapergem/draper

    EDIT: Looks like the accepted answer basically did suggest doing something like this. But yeah, you want to use decorators. Here's a great tutorial series to help you understand more:

    https://gorails.com/episodes/decorators-from-scratch?autoplay=1

    P.S. - @excid3 I accept free membership months LOL

    0 讨论(0)
提交回复
热议问题