代码之家  ›  专栏  ›  技术社区  ›  Gav

在轨道中干燥视图(从数字到货币)

  •  10
  • Gav  · 技术社区  · 15 年前

    我的代码类似于:

    number_to_currency(line_item.price, :unit => "£")

    把我的观点乱扔在各种模型上。由于我的应用程序仅以英镑(_)交易,我是否应该将此移动到我的每个模型中,以便 line_item.price 按原样返回字符串(即 数字“U to”货币(行“U item.price”,单位=gt;“_”) 价格指数 都一样。我认为要做到这一点,我应该:

    def price
     number_to_currency(self.price, :unit => "£")
    end
    

    但这不管用。如果 price 已经在模型中定义了,当我更改时,Rails报告“堆栈级别太深” def price def amount ,然后它抱怨说 number_to_currency 没有定义?

    6 回复  |  直到 14 年前
        1
  •  13
  •   Larry K    15 年前

    数字货币是视图助手,因此在模型中不可用。

    您可以通过在application_helper.rb中定义自己的助手来保存一些关键的笔画(这样它对所有视图都可用)。如

    def quid(price)
      number_to_currency(price, :unit => "£")
    end
    

    然后在视图中调用它:

    quid(line_item.price)
    
        2
  •  41
  •   Josh    14 年前

    如果要更改整个应用程序的默认值,可以编辑config/locales/en.yml

    我的看起来像这样:

    # Sample localization file for English. Add more files in this directory for other locales.
    # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
    "en":
      number:
        currency:
            format:
                format: "%u%n"
                unit: "£"
                # These three are to override number.format and are optional
                separator: "."
                delimiter: ","
                precision: 2
    

    除了单位之外的所有内容都是可选的,并将返回到默认值,但我将其放入,以便知道可以更改哪些值。您也可以使用_符号而不是£。

        3
  •  6
  •   mikej heading_to_tahiti    15 年前

    原因是 堆栈级别太深 错误是当你说 self.price price 方法您正在创建对price方法的无限递归调用,因为您现在已经重写了普通的访问器方法。为了避免这种情况,您需要使用属性散列访问price字段的值。例如:

    def price
     number_to_currency(attributes['price'], :unit => "£")
    end
    

    但事实上 number_to_currency 由于Larry K描述的原因,在模型代码中不可用。

        4
  •  2
  •   Jason Cale    15 年前

    这是我解决这个问题的方法。

    # /RAILS_ROOT/lib/app_name/currency_helper.rb
    module AppName
      module CurrencyHelper    
    
        include ActionView::Helpers::NumberHelper
    
        def number_to_currency_with_pound(amount, options = {})
          options.reverse_merge!({ :unit => '£' })
          number_to_currency_without_pound(amount, options)
        end
    
        alias_method_chain :number_to_currency, :pound
    
      end
    end
    

    在您的模型中,您可以这样做(并且您不会用您不打算使用的方法污染您的模型)

    class Album < ActiveRecord::Base
      include AppName::CurrencyHelper
    
      def price
        currency_to_number(amount)
      end
    end
    

    然后,要更新所有视图,请在应用程序帮助程序中包含该模块。

    module ApplicationHelper
       # change default currency formatting to pounds..
       include AppName::CurrencyHelper
    end
    

    现在,无论您在何处使用数字货币助手,它都将使用磅符号进行格式化,但是您还具有原始Rails方法的所有灵活性,因此您可以像以前那样传入选项。

    number_to_currency(amount, :unit => '$')
    

    将它转换回美元符号。

        5
  •  1
  •   Mark Connell    15 年前

    另一个关于让另一个助手方法quid(price)简化重复的答案可能是最好的方法。然而。。如果您真的想访问模型中的视图帮助程序,可以执行如下操作:

    # /RAILS_ROOT/lib/your_namespace/helper.rb
    #
    # Need to access helpers in the model?
    # YourNamespace::Helper.instance.helper_method_name
    module YourNamespace
      class Helper
        include Singleton
        include ActionView::Helpers
      end
    end
    

    然后您应该能够在模型类中这样做:

    def price
      helper = YourNamespace::Helper.instance
      helper.number_to_currency(read_attribute('price'), :unit => "£")
    end
    
        6
  •  1
  •   user512087    14 年前

    至于轨道3

    正如Larry K所描述的,但有了这个编辑:

    def quid(price)
       number_to_currency(price, :unit => "&pound;")
     end