2016-04-09 27 views
0

我正在關注本教程http://pauldeardorff.com/2013/08/14/handling-currencies-in-ruby-on-rails-apps/關於在rails應用程序中添加價格和貨幣選擇。在rails應用程序中處理價格和貨幣

在啓動應用程序我有我的模塊設置完全一樣的簡單教程

我的問題似乎當我第一次使我的產品類的新動作到達。本教程建議在rails Lib文件夾中創建一個money_attributes文件。

# lib/money_attributes.rb 
module MoneyAttributes 
    extend ActiveSupport::Concern 
    module ClassMethods 
    def money_attributes *args  
     args.each do |attribute| 
     cents_attribute = "#{attribute}_in_cents" 
     define_method attribute do 
      send(cents_attribute).try("/", 100.0) 
     end 

     define_method "#{attribute}=" do |value| 
      value.gsub!(/[^\d.]/,"") if value.is_a? String   
      send("#{cents_attribute}=", value.try(:to_f).try("*", 100)) 
     end 

     define_method "#{attribute}_money" do         
      Money.new(send("#{attribute}_in_cents").to_f || 0,send("currency")).format 
     end 

     attr_accessible attribute if accessible_attributes.include?(cents_attribute) 
     end 
    end 
    end 
end 

我的應用吐出未定義的局部變量或方法`accessible_attributes'。

我有一個什麼是錯的想法,因爲'accessible_attributes'在我認爲的rails 4中停用。我的問題是我不知道如何解決這個問題。另外我的控制強參數看起來如下

 products_controller.rb 

...... 私人

def product_params 
     params.require(:product).permit(:price, :currency) 
    end 

如果任何人有關於如何解決這個問題,我會非常感激的想法或可能有記住與教程匹配的更好的方法。我的問題是,我希望價格和貨幣選擇在我的模型表單中分開。就像本教程中指定的一樣。任何幫助將不勝感激。

回答

0

屬性保護現在在控制器中完成(通過強參數,因爲您已實施),因此可以刪除attr_accessible attribute if accessible_attributes.include?(cents_attribute)行。