-
-
Save RainMonster/6236342 to your computer and use it in GitHub Desktop.
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
| $(document).ready(function () { | |
| $('form').submit( function(e) { | |
| e.preventDefault(); | |
| var value = Math.floor(Math.random() * 6 + 1); | |
| console.log('the client side value was: ' + value); | |
| var request = $.ajax({ | |
| url: this.action, | |
| type: this.method, | |
| data: { value: value } | |
| }); | |
| request.done( function(data) { | |
| var die = $('#die img'); | |
| die.attr('src', '/' + data + '.png'); | |
| die.attr('title', data); | |
| $('#die').show(); | |
| }); | |
| }); | |
| }); |
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
| <div class="container"> | |
| <h1>Simplest Possible AJAX</h1> | |
| <p>This contrived app will simulate a roll of a 6-sided die.</p> | |
| <form method="post" action="/rolls"> | |
| <input type="submit" value="Roll the Die"> | |
| </form> | |
| <div id="die" style='display: none'> | |
| <img alt="the roll"> | |
| </div> | |
| </div> |
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
| get '/' do | |
| erb :index | |
| end | |
| post '/rolls' do | |
| params[:value] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment