12

我的代码类似于:

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

在各种模型中乱扔我的观点。由于我的应用程序仅以英镑(£)交易,我是否应该将其移至我的每个模型中,以便line_item.price返回应为的字符串(即number_to_currency(line_item.price, :unit => "£")并且line_item.price是相同的。我认为要做到这一点,我应该:

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

但这不起作用。如果price模型中已经定义了,那么 Rails 会报告“堆栈级别太深”,当我更改def price为时def amount,它会抱怨number_to_currency未定义?

4

6 回答 6

41

If you want to change the default for your whole application, you can edit config/locales/en.yml

Mine looks like this:

# 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

Everything except the unit is optional and will fall back to the default, but I put it in so I know what values I can change. you could also use the £ sign instead of £.

于 2010-12-14T03:01:10.893 回答
13

number_to_currency 是视图助手,因此在模型中不可用。

您可以通过在 application_helper.rb 中定义自己的助手来节省一些击键(因此它可用于所有视图)。例如

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

然后在视图中调用它:

quid(line_item.price)
于 2009-09-09T15:14:45.820 回答
6

堆栈级别太深错误的原因是,当您self.priceprice方法中说您正在创建对价格方法的无限递归调用时,因为您现在已经覆盖了正常的访问器方法。为避免这种情况,您需要使用属性哈希访问价格字段的值。例如:

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

number_to_currency除了由于拉里 K 描述的原因在模型代码中不可用的事实。

于 2009-09-09T15:18:39.243 回答
2

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

# /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

in your models you can do this (and you won't be polluting your model with methods you aren't going to use)

class Album < ActiveRecord::Base
  include AppName::CurrencyHelper

  def price
    currency_to_number(amount)
  end
end

then for your views to all be updated include the module in one of your app helpers

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

Now everywhere you use the number to currency helper it will be formatted with a pound symbol, but you also have all the flexiblity of the original rails method so you can pass in the options as you did before ..

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

will convert it back to a dollar symbol.

于 2009-11-03T11:17:31.217 回答
1

关于制作另一个帮助器方法 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
于 2009-09-09T15:27:49.963 回答
1

As of Rails 3

As Larry K describes but with this edit:

def quid(price)
   number_to_currency(price, :unit => "&pound;")
 end
于 2011-02-09T17:12:40.233 回答