我正在尝试在我的应用程序中实现this。
这篇文章说我必须创建一个装饰器,但它并没有详细说明如何做到这一点。代码如下:
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)我试着运行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!'解决这个问题的最好方法是什么?
发布于 2013-05-08 05:02:39
您应该将上述代码添加到app/decorators/cart_decorator.rb中
装饰器文件夹将是新的,并在启动rails应用程序时自动加载。当它运行Piggybak::Cart.send(:include, CartDecorator)时,它会用你上面声明的方法装饰你的Piggybag::Cart。
https://stackoverflow.com/questions/16428429
复制相似问题