Jackson Cunningham
Jackson Cunningham

Reputation: 5073

Webhooks in Rails Controller for Shopify App

I have a webhooks controller which listens for when a new customer is created in 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. 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..

Hope those make sense.

Upvotes: 1

Views: 649

Answers (1)

Gavin Terrill
Gavin Terrill

Reputation: 1039

It seems like I should be handling the customer creation in Customer#create

Where the code lives is up to you but you must keep it 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:

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.

Upvotes: 2

Related Questions