在不同的控制器中使用 Shopify API

Using Shopify API in different Controller

我需要获取当前商店的域名(我使用 ShopifyAPI::Shop.current.domain 执行此操作)。这在 HomeController 中有效,可以在 home/index.html.erb 中显示。但是,当我尝试在我的 CustomController 中调用 ShopifyAPI::Shop.current.domain 以在不同的视图中显示时,我收到错误 Missing site URI.

我认为这是因为一旦用户离开 home/index 视图,它就无法再访问该 Shop 实例。那么,如果我不知道 id,我该如何在我的 CustomController 中重新创建该实例。例如,这有效:

@shop = Shop.find(1)
@domain = @shop.shopify_domain

但是,我不会一直知道模型中店铺的id

或者,也许我的处理方式有误。

派对迟到了,但在您的控制器中使用 around_filter :shopify_session 应该可以解决此问题。如果没有,请确保控制器继承自 AuthenticatedController.

示例:

class CustomerController < AuthenticatedController
  around_filter :shopify_session

  def shops
    @shop = ShopifyAPI::Shop.find(1)
  end
end