How to use Rails to post to Woocommerce REST API

admin

Administrator
Staff member
I'm trying write code using the Woocommerce REST API v. 3 with Ruby on Rails 6.

The Woocommerce shop is install by MAMP and run on localhost:8888 and Rails runs on localhost:3000.

I'm using the "<a href=" " rel="nofollow noreferrer">WooCommerce API - Ruby Client</a>" gem.

I just speedcoded to have something working and the refactor so this is my code right now:

Code:
class ProductsController &lt; ApplicationController
  require 'woocommerce_api'
  require 'json'

  def new
    @product = Product.new
  end

  def create
    woocommerce = WooCommerce::API.new(
      'http://localhost:8888/wordpress_rails/wordpress/wp-json/wc/v3/products',
      'CLIENT_KEY',
      'CLIENT_SECRET',
      version: 'v3',
      wp_api: true,
      debug_mode: true
    )
    data = {
      name: 'Premium Quality',
      type: 'simple',
      regular_price: '290',
      description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada 
      fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor 
      sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris 
     placerat eleifend leo.',
  short_description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.'

    }

woocommerce.post('products', data).parsed_response
  end
end

When I post
Code:
data
with Postman to the same URL as above, the product is created but when I'm doing a post with Rails I get the
Code:
rest_no_route
error.

Here is my post with Postman:

<a href=" " rel="nofollow noreferrer"><img src=" " alt="Create new product with Postman"></a>

What is going wrong when I post to the REST API with Rails?