【发布时间】:2016-04-09 10:01:58
【问题描述】:
我正在关注本教程http://pauldeardorff.com/2013/08/14/handling-currencies-in-ruby-on-rails-apps/,了解如何在 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”。
我知道出了什么问题,因为我相信在 rails 4 中“accessible_attributes”已停止使用。我的问题是我不知道如何解决这个方法。我的控制强参数也如下所示
products_controller.rb
…… 私人的
def product_params
params.require(:product).permit(:price, :currency)
end
如果有人知道如何解决这个问题,我将不胜感激,或者可能有更好的方法与本教程相匹配。我的问题是我希望在我的模型表单中单独选择价格和货币。就像本教程中指定的一样。任何帮助将不胜感激。
【问题讨论】:
标签: ruby-on-rails forms ruby-on-rails-4 currency rails-models