我正在尝试在我的应用程序中实现这一点。
文章说我必须创建一个装饰器 - 但它并没有详细说明如何做到这一点。这是代码:
module CartDecorator
extend ActiveSupport::Concern
module InstanceMethods
def is_downloadable?
items = self.items.collect { |li| li[:variant].item }
items.all? { |i| i.is_downloadable }
end
def has_downloadable?
items = self.items.collect { |li| li[:variant].item }
items.any? { |i| i.is_downloadable }
end
end
end
Piggybak::Cart.send(:include, CartDecorator)
我不确定是否应该将该代码添加到某些代码中model.rb
(对于它的价值,我的文件夹中没有piggybak_cart.rb
)app/models/
。
我试过跑步rails g decorator Cart
,但没有用。
我所做的是将上面的代码放入app/helpers/cart_helper.rb
.
然后,当我尝试运行rails g command
(用于其他)时,我现在收到此错误:
/.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant CartHelper (NameError)
from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:136:in `block in modules_for_helpers'
from /.rvm/gems/ruby-2.0.0-p0@myapp/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:131:in `map!'
解决这个问题的最佳方法是什么?