最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to develop rails app for shopify with ScriptTags - Stack Overflow

programmeradmin1浏览0评论

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 ( ), but I don't understand it...

Where do I have to put this code:

HTTP POST 

  <?xml version="1.0" encoding="UTF-8"?>
  <script-tag>
    <src>.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?

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./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./admin/scripttags

  <?xml version="1.0" encoding="UTF-8"?>
  <script-tag>
    <src>http://example./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?

Share Improve this question edited Aug 13, 2012 at 15:08 kungfucsiga asked Aug 13, 2012 at 14:22 kungfucsigakungfucsiga 3391 gold badge4 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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!

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./ajax/libs/jquery/1.7.2/jquery.min.js")

You need to replace https://ajax.googleapis./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!

发布评论

评论列表(0)

  1. 暂无评论