0
我想在我的應用程序中實現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)
我不知道我是否應該該代碼添加到一些model.rb
(對於它的價值,我不會在我的app/models/
文件夾中有一個piggybak_cart.rb
做)。
我試着運行rails g decorator Cart
,但沒有奏效。
我所做的是把上面的代碼放在app/helpers/cart_helper.rb
。
然後,當我試圖運行一個rails g command
(別的東西),我現在收到此錯誤:
/.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant CartHelper (NameError)
from /.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `each'
from /.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:229:in `constantize'
from /.rvm/gems/[email protected]/gems/activesupport-3.2.13/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
from /.rvm/gems/[email protected]/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:136:in `block in modules_for_helpers'
from /.rvm/gems/[email protected]/gems/actionpack-3.2.13/lib/abstract_controller/helpers.rb:131:in `map!'
什麼是接近最好的方法?
非常感謝。正是醫生要求的:) – marcamillion 2013-05-07 21:19:52