Skip to content

Instantly share code, notes, and snippets.

@Lax
Created August 14, 2017 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lax/3a3b2367b8ab9b3f6c1864b5e7c1c33c to your computer and use it in GitHub Desktop.
Save Lax/3a3b2367b8ab9b3f6c1864b5e7c1c33c to your computer and use it in GitHub Desktop.
Demo code for `rails-ujs`
#
# Usage:
# rails new -m=ujs.rb ujs_demo
#
gem 'rails'
gem 'jquery-rails'
generate :controller, :ujs
inject_into_class 'app/controllers/ujs_controller.rb', 'UjsController', <<-CODE
def index
end
def json
render json: {name: 'Hello world!'}
end
CODE
route %q{get 'ujs/json', as: :json}
route %q{root to: 'ujs#index'}
file 'app/views/ujs/index.html.erb', <<-CODE
<%= link_to :json, :json, data: {remote: true, url: json_path, type: :json} %>
CODE
insert_into_file 'app/assets/javascripts/application.js', "//= require jquery\n", before: %r^//= require rails-ujs^
append_to_file 'app/assets/javascripts/ujs.coffee', <<-CODE
$(document).on "turbolinks:load", ->
$("a[data-remote]").on 'ajax:success', (e, a, b, c) ->
alert JSON.stringify([e, a, b, c])
CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment