2009-09-09 90 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