问题
I have a shopify app deployed in Heroku, developed in Rails. I need call a javascript function from any shopify store's frontend. I've read this article ( http://www.shopify.com/technology/3033852-shopify-api-update-scripttags-javascript-insertion ), but I don't understand it...
Where do I have to put this code:
HTTP POST http://their-shop.myshopify.com/admin/scripttags
<?xml version="1.0" encoding="UTF-8"?>
<script-tag>
<src>http://example.com/your-awesome-script.js</src>
<event>onload</event>
</script-tag>
What should be the src tag (for my deployed rails app), and where should I put the javascript call function?
回答1:
This might help.
Shopify Rails App
You simply need to authenticate user and then you need to take the token generated from Shopify and save it in your Database. Later on just do:
ShopifyAPI::Base.site = token
and then install the script using:
s = ShopifyAPI::ScriptTag.create(:events => "onload",:src => "your javascript url")
and you are done!
回答2:
This is what I did to get it working:
In the sessions_controller.rb file located in app > controllers
After this
def show
if response = request.env['omniauth.auth']
sess = ShopifyAPI::Session.new(params[:shop], response[:credentials][:token])
session[:shopify] = sess
I added
ShopifyAPI::Base.activate_session(sess)
ShopifyAPI::ScriptTag.create(:event => "onload", :src => "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js")
You need to replace https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
in the code above with your absolute js file path.
Then in the omniauth.rb file located in config > initializers
make sure you add write_script_tags
to the scope i.e.
:scope => 'write_script_tags',
and you might also need to restart your rails server and re-install the app so that these privileges have been granted. That should do the trick!
来源:https://stackoverflow.com/questions/11936268/how-to-develop-rails-app-for-shopify-with-scripttags