I am triyng to create a new Product to the shopify API.
when i put this in my controller:
new_product = ShopifyAPI::Product.new
new_product.title = "Burton Custom Freestlye 151"
new_product.product_type = "Snowboard"
new_product.vendor = "Burton"
new_product.save
It will create a new product on each page reload
I want to create a product with a form to fill out on the front end.
I have tried this:
controller:
@new_product = ShopifyAPI::Product.new(
:title => :title,
:vendor => :vendor,
:product_type => :product_type,
)
View:
<form method="POST" action=<%= @new_product %> data-shopify-app-submit="">
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
<p>
<label>Name:</label>
<input type="text" name="title"/>
</p>
<p>
<label>Body</label>
<input type="text" name="body_html"/>
</p>
<p>
<label>Vendor:</label>
<input type="text" name="vendor"/>
</p>
<p>
<label>Product Type:</label>
<input type="text" name="product_type"/>
</p>
<p>
<label>Tags:</label>
<input type="text" name="tags"/>
</p>
<input type="submit">
</form>
Error:
ActionController::RoutingError (No route matches [POST] "/"):
I then tried adding post '/'
to my routes but it wouldn’t commit.
I added post 'admin/api/products'
as well to my routes, but no help.
The API route to POST to is https://#{@shop_session.url}/admin/api/products.json
so i tried using this form:
<form method="POST" action="<%= "https://#{@shop_session.url}/admin/api/products.json" %>" data-shopify-app-submit="">
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
....
.....
<input type="submit">
</form>
With this, nothing happens. Not even an update within heroku logs. But in firefox i get a “Blocked by Content Security Policy”.
How can I convert the working code in the controller that works for reloads into a form I can use on the front end?
2
Answers
You have to define “front-end”. What does that mean? In your App all your routes are relative to the App itself, which is hopefully embedded in the shop itself. When you have a form for Add Product in your App, you POST to whatever endpoint you setup in your controller scheme for that. So your front-end is just a view in your App.
If instead by “front-end” you mean the customer facing Shopify store itself, then that is never going to work securely except in the case of using the App Proxy pattern, for obvious reasons.
When you write
@new_product
in your controller, there is likely to be no value returned, so when you access it in your form action, it defaults to “/”.I would suggest creating a route for your controller and action in your
routes.rb
file. For example, if your controller isProductsController
and the action (or method) isdef create
, the route would look likePOST /products => products#create
.Then, in your form, you can use
/products
in your form action, like:<form method="POST" action="/products" data-shopify-app-submit="">