Created
August 14, 2017 04:43
-
-
Save Lax/3a3b2367b8ab9b3f6c1864b5e7c1c33c to your computer and use it in GitHub Desktop.
Demo code for `rails-ujs`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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