2009-09-09 80 views
9

我有类似的代码:干燥意见

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 pricedef amount,然后抱怨说number_to_currency没有定义?

+1

如果你可以只设置默认单位GBP并直接使用number_to_currency是不是越干? – 2009-09-20 18:33:50

回答

12

number_to_currency是一个视图助手,所以它不适用于模型。

您可以通过在application_helper.rb中定义自己的帮助程序来保存一些关键笔划(以便它可用于所有视图)。例如

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

然后调用它的观点:

quid(line_item.price) 
6

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

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

除了事实number_to_currency不是拉里ķ描述的原因型号代码可用。

+0

感谢您向我解释递归调用位。 – Gav 2009-09-09 15:27:43

1

关于制作另一个助手方法镑(价​​格),以简化重复对方回答可能是最好的办法..但是..如果你真的想访问你可以做类似的模型视图助手:

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

在你的模型,你可以做到这一点(和你会不会跟你不打算使用方法污染模型)

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 

现在无处不在,你使用的货币助手就会以英镑符号格式化的数字,但你也有原来的轨道方法的所有flexiblity,所以你可以在选项为你传递以前做过..

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

会将其转换回美元符号。

41

如果要更改默认的为您的整个应用程序,您可以编辑配置/区域设置/ 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: "&pound;" 
      # These three are to override number.format and are optional 
      separator: "." 
      delimiter: "," 
      precision: 2 

一切都是可选的,回到默认值,但是我把它放在里面,所以我知道我可以改变什么值。你也可以使用英镑而不是&磅。

+0

感谢您的提示; “ActionView :: Helpers :: NumberHelper number_to_currency()”的Rails API文档给出了一个例子,其中设置':locale =>“fr”'更改分隔符和分隔符以及货币符号位置。这不适合我,但通过添加一个像上面这样的设置的'config/locales/fr.yml',它开始工作。 – bjnord 2011-06-26 01:16:34

+2

我总是使用一个自定义的助手,类似于Larry K的接受答案所描述的,但这是一个更加清洁的方法。 – 2011-08-31 07:31:04

1

铁轨3

拉里的K描述,但这种编辑:

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