【发布时间】:2014-10-07 10:48:49
【问题描述】:
我正在现有的 Rails 应用程序中构建一个 spree 商店,我需要从 Spree 引擎外部访问 link_to_cart。
link_to_cart 可以在这里找到:spree/core/app/helpers/spree/base_helper.rb
由于我修改了link_to_cart中的样式,我也创建了:
#helpers/spree/base_helper_decorator.rb
module Spree
module BaseHelper
def link_to_cart(text = nil)
text = text ? h(text) : Spree.t('cart')
css_class = nil
if simple_current_order.nil? or simple_current_order.item_count.zero?
text = "#{text}: (#{Spree.t('empty')})"
css_class = 'empty'
else
text = "<i class='fa fa-shopping-cart'></i> #{text}: (#{simple_current_order.item_count}) <span class='amount'>#{simple_current_order.display_total.to_html}</span>".html_safe
css_class = 'full'
end
link_to text.html_safe, spree.cart_path, :class => "cart-info #{css_class} btn btn-small btn-success pull-right", style: "margin-left:10px;"
end
end
end
我曾尝试在引擎之外执行诸如 Spree::BaseHelper.link_to_cart 之类的操作,但我不断收到 undefined local variable or method 'link_to_cart'
我在different StackOverflow question 上找到了这个,看起来很有希望,但我不知道如何根据我的需要修改它:
module MyEngine
class Engine < Rails::Engine
initializer 'my_engine.action_controller' do |app|
ActiveSupport.on_load :action_controller do
helper MyEngine::ImportantHelper
end
end
end
end
【问题讨论】:
-
嗨@Abram,我有同样的问题,但解决方案对我不起作用。有什么建议吗?
标签: ruby-on-rails spree