如何从主rails应用程序访问Spree的link_to_cart函数

时间:2014-08-13 22:03:36

标签: ruby-on-rails spree

我正在为现有的Rails应用程序构建一个狂欢商店,我需要从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

5 个答案:

答案 0 :(得分:6)

好的,谢谢本让我走上正轨。这是我的解决方案:

# class ApplicationController < ActionController::Base
   include Spree::Core::ControllerHelpers::Order
   include Spree::Core::ControllerHelpers::Auth
   helper Spree::BaseHelper
   helper Spree::StoreHelper

<强>更新

我遇到了current_store在引擎外部未定义的问题。我不确定如何正确解决这个问题,但与此同时我刚刚添加以下内容以阻止spree调用current_store:

module Spree
  module Core
    module ControllerHelpers
      module Order
        def current_order_params
          { currency: current_currency, guest_token: cookies.signed[:guest_token], store_id: Spree::Store.first, user_id: try_spree_current_user.try(:id) }
        end
      end
    end
  end
end

此外,似乎不再需要helper Spree::StoreHelper来显示购物车按钮和当前订单..

答案 1 :(得分:1)

你需要做两件事

  1. 创建覆盖
  2. 在主要内容中访问您的覆盖 应用
  3. 第1步:我的Spree覆盖位于主应用程序的覆盖文件夹中。覆盖应该在您正在装饰的模块上调用module_eval

    #overrides/base_helper_decorator.rb
    Spree::BaseHelper.module_eval do
    
      def link_to_cart(text = nil)
        #Your customizations here
      end
    
    end
    

    步骤2:将以下一行添加到主应用AppController以访问您的装饰助手。

    helper_method :link_to_cart # Add only the link_to_cart method 
    
    helper 'spree/base'         # Add all methods (your decoration plus the default methods) 
                                # from the Spree BaseHelper module to your main app
    

答案 2 :(得分:1)

自从亚伯兰离开他的答案后,有一些更新。它让我走上正轨,但我有一些打嗝。主要的是current_currency未定义。

application_controller.rb

class ApplicationController < ActionController::Base
  include Spree::Core::ControllerHelpers::Order
  include Spree::Core::ControllerHelpers::Auth
  include Spree::Core::ControllerHelpers::Store
  include Spree::Core::ControllerHelpers::Common
  helper Spree::BaseHelper

我覆盖了Spree导航栏并将其用于我的主应用程序。

我添加include Spree::Core::ControllerHelpers::Common后就开始工作了。不幸的是,这会通过spree spree_application.html.erb布局呈现所有视图。您可能必须使用此视图覆盖并修改一下。

此外,此时所有的css都来自狂欢。您必须将自定义css移动到spree名称空间并@import it。

答案 3 :(得分:0)

您可以将其添加到main_app以访问购物车。

<%= link_to "Cart", spree.cart_path %>

答案 4 :(得分:0)

您还必须在引擎外部的主应用程序中包含正确的帮助程序。在即将发布的版本3.2.0中,我只需要添加:

# class ApplicationController < ActionController::Base
  include Spree::Core::ControllerHelpers::Store
  include Spree::Core::ControllerHelpers::Order
  include Spree::Core::ControllerHelpers::Auth

到我的ApplicationController,使link_to_cart无处不在。您需要包含多个辅助类,因为例如current_store(由其他帮助程序使用)在Spree::Core::ControllerHelpers::Store中定义,因此添加它也可以消除任何错误。