简体   繁体   English

用于Shopify应用程序的Rails Controller中的Webhooks

[英]Webhooks in Rails Controller for Shopify App

I have a webhooks controller which listens for when a new customer is created in Shopify. 我有一个webhooks控制器,用于在Shopify中创建新客户时进行监听。

def customer_created_callback
    @first_name = params[:first_name]
    @last_name = params[:last_name]
    @email = params[:email]

    @customer = Customer.new(first_name: @first_name, last_name: @last_name, email: @email)

    if @customer.save
      redirect_to customers_url
    else
      puts "error"
    end

  end
  1. I am wondering if I should be creating the customer in this controller action. 我想知道是否应该在此控制器操作中创建客户。 It seems like I should be handling the customer creation in Customer#create. 似乎我应该在Customer#create中处理客户创建。 But I'm not sure if it's a good idea to be passing all these params to a new action. 但是我不确定将所有这些参数传递给新动作是否是一个好主意。

  2. What's a good example of where I should be redirecting_to? 我应该重定向到哪里的一个好例子是什么? I thought the idea of a webhook is that it's happening in the background so no page is actually going to be rendered.. 我以为Webhook的想法是它发生在后台,因此实际上不会呈现任何页面。

Hope those make sense. 希望那些有道理。

It seems like I should be handling the customer creation in Customer#create 似乎我应该在Customer#create中处理客户创建

Where the code lives is up to you but you must keep it DRY . 代码所处的位置由您决定,但必须保持DRY Personally I like to use Service Objects since they make testing easier. 我个人喜欢使用服务对象,因为它们使测试更加容易。

What's a good example of where I should be redirecting_to? 我应该重定向到哪里的一个好例子是什么?

You need to return a 200 response with no content: 您需要返回没有内容的200响应:

render :nothing => true, :status => 200

Typically you'll use a background job which will use the service object when it runs. 通常,您将使用后台作业,该后台作业将在服务对象运行时使用。 This post on Webhook Best Practices is an excellent resource to get acquainted with the in's and out's of web hooks. Webhook最佳实践上的这篇帖子是了解Web钩的入钩和出钩的绝佳资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM