How to create a Github gist-like embeddable widget using Rails
Github gists are great and easy to share. Simply paste their embed code onto any page.
<!-- Embed code -->
<script src="https://gist.github.com/BrianSigafoos/f0fe5e6417b0acf70ffe1bc6b6498796.js"></script>
The response from that script simply does 2 things using document.write()
:
// Endpoint Response to .js code above
document.write(
'<link rel="stylesheet" href="https://github.githubassets.com/assets/gist-embed-5687a589e344.css" />'
)
document.write(
'<div id="gist74791356" class="gist">\n <div class="gist-file" translate="no">...</div></div></ div>\n'
)
The code to embed the widget is simple:
<div class="demoapp demoapp-widget">
<script src="http://lvh.me:3000/widgets/mKVEFN.js"></script>
<p>Widget created with <a href="https://github.com/BrianSigafoos/docker-rails-webpacker-app">Docker Rails Demo App</a> by Brian</p>
</div>
And here is a widget rendered below. Note: this only works locally for now via this repo.
resources :widgets, only: :show
skip_before_action :verify_authenticity_token
and layout false
with a show.js.erb like thisrack-cors
gem to allow CORS with a config/initializers/cors.rb like this:where(.demoapp) { ... }
, for example