【问题标题】:How to access Spree's link_to_cart function from main rails app如何从主 Rails 应用程序访问 Spree 的 link_to_cart 功能
【发布时间】: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


【解决方案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 都来自 spree。您必须将您的自定义 css 移动到 spree 命名空间并@import 它。

【讨论】:

    【解决方案2】:

    您还必须在引擎之外的主应用程序中包含正确的助手。在即将发布的 spree 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 中定义,所以也添加它以消除任何错误。

    【讨论】:

      【解决方案3】:

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

      &lt;%= link_to "Cart", spree.cart_path %&gt;

      【讨论】:

        【解决方案4】:

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

        # 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 似乎不再需要显示购物车按钮和当前订单..

        【讨论】:

        • 啊哈,传说中的 stackoverflow 答案与您需要的确切答案!请问您在哪里添加了第二个代码sn-p?我试过订单控制器装饰器但没有雪茄:(
        • 无法回忆。试试 lib
        • 嗯,不走运。在应用程序中尝试了各种组合,也没有运气。多么奇怪。
        • 将 current_order_params 函数添加到应用程序控制器并手动指定 current_currency 目前对我有用!
        • 谢谢!它让我走上了正轨。似乎对狂欢进行了一些更新,因此您必须添加 include Spree::Core::ControllerHelpers::Common 。不过,这会改变整个应用的呈现方式。
        【解决方案5】:

        你需要做两件事

        1. 创建您的覆盖
        2. 使您的覆盖可在主目录中访问 应用

        第 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 步:将以下行之一添加到您的主应用 ApplicationController 以访问您的装饰助手。

        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
        

        【讨论】:

        • 不知道为什么,但在做了所有事情之后,我仍然得到未定义的局部变量或方法 `link_to_cart'
        猜你喜欢
        • 2012-06-23
        • 1970-01-01
        • 2018-01-27
        • 1970-01-01
        • 1970-01-01
        • 2022-11-29
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        相关资源
        最近更新 更多