Shopify App导致返回代码302

时间:2016-10-23 17:48:08

标签: ruby-on-rails shopify

我有这段代码在我的Shopify商店中创建新订单时触发:

class CustomwebhooksController < ShopifyApp::WebhooksController 
  #the line below is to avoid the CSR error
  protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }

  include ShopifyApp::WebhookVerification

  # This method is triggered when the user completes the checkout process
  # Gets the order number and see the message "Your order is confirmed" 
  # I should use this as the trigger to update the other SKU for the DT solution:
  def orders_create
    request.body.rewind
    data = JSON.parse(request.body.read)

    #redirect_to controller: "home", action: "update_stock", status: :found

    @MyHomeController = HomeController.new
    @MyHomeController.update_stock

    #do not remove line below
    head :ok

  end

end

这是我家控制器中的代码:

class HomeController < ShopifyApp::AuthenticatedController
  def update_stock
    @products = ShopifyAPI::Product.find(:all, :params => {:title => 'DT382'})
    @products.each do |product|
      puts(product.id)
    end  
  end      
end

以某种方式调用@MyHomeController.update_stock会生成302,如下所示:

渲染内联模板(0.4ms)   完成302发现在689ms(浏览次数:0.9ms | ActiveRecord:0.0ms)

我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

我想挑战你的设计一分钟。

首先,如果您要重定向到其他控制器/操作,为什么不使用路由文件中指定路径的redirect_to update_stock_path

其次,为什么要重定向?您正在接收带有一些有效负载的webhook,然后您需要点击Shopify API来获取产品数据。对我来说,这更适合作为后台工作。此外,将其设计为receive webhook -> schedule background job -> do work in the job将比您现在所做的更具可扩展性。

答案 1 :(得分:0)

在仔细阅读Shopify API文档后,我设法找到了解决方案。

class HomeController&lt; ShopifyApp :: AuthenticatedController   def update_stock     shop_url =“https://# {ENV ['API_KEY']}:#{ENV ['API_SECRET']} @ myshopifyshop.myshopify.com/admin”     ShopifyAPI :: Base.site = shop_url     @products = ShopifyAPI :: Product.find(:all,:params =&gt; {:title =&gt;'DT382'})     @ products.each do | product |       看跌期权(product.id)     结束
   ShopifyAPI :: Base.clear_session   结束