在其他控制器中使用Shopify API [英] Using Shopify API in different Controller

查看:148
本文介绍了在其他控制器中使用Shopify API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取当前商店的域名(我使用ShopifyAPI::Shop.current.domain来完成此操作).这可以在HomeController中工作,并且可以在home/index.html.erb中显示它.但是,当我尝试在CustomController中调用ShopifyAPI::Shop.current.domain以在其他视图中显示时,出现错误Missing site URI.

I need to get the current shop's domain (I do this using ShopifyAPI::Shop.current.domain). This works in the HomeController and can display it in home/index.html.erb. But, when I try to call ShopifyAPI::Shop.current.domain in my CustomController to display in a different view, I get the error Missing site URI.

我认为这是因为一旦用户离开主页/索引视图,它就不再具有对该Shop实例的访问权限.因此,如果我不知道ID,该如何在CustomController中重新创建该实例.例如,这有效:

I think this is because once the user leaves the home/index view it no longer has access to that Shop instance. So, how can I recreate that instance in my CustomController if I don't know the id. For example, this works:

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

但是,我不会总是知道模型中商店的id.

But, I will not always know the shop's id in the model.

或者,也许我正在以错误的方式来对待.

Or, maybe I am approaching this the wrong way.

推荐答案

迟到聚会,但是在控制器中使用around_filter :shopify_session应该可以解决此问题.如果不是,请确保控制器从AuthenticatedController继承.

Late to the party but using around_filter :shopify_session in your controller should fix this. If it doesn't, ensure that the controller is inheriting from the AuthenticatedController.

示例:

class CustomerController < AuthenticatedController
  around_filter :shopify_session

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

这篇关于在其他控制器中使用Shopify API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆