如何从主rails应用程序访问Spree的link_to_cartfunction
我正在为一个现有的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 = " #{text}: (#{simple_current_order.item_count}) #{simple_current_order.display_total.to_html}".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'
我在一个不同的StackOverflow问题上发现了这个问题 ,看起来很有希望,但我不确定如何根据我的需要修改它:
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
好的,谢谢本让我走上正轨。 这是我的解决方案:
# 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步:我的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
自从亚伯兰离开他的答案后,有一些更新。 它让我走上了正确的轨道,但我有一些打嗝。 主要的是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它。
您可以将其添加到main_app以访问购物车。
<%= link_to "Cart", spree.cart_path %>
您还必须在引擎外部的主应用程序中包含正确的帮助程序。 在即将发布的版本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
,因此添加它也可以消除任何错误。