Skip to content

Instantly share code, notes, and snippets.

@TravisL12
Forked from dbc-challenges/lucky_ajax.md
Created May 15, 2013 02:54
Show Gist options
  • Save TravisL12/5581354 to your computer and use it in GitHub Desktop.
Save TravisL12/5581354 to your computer and use it in GitHub Desktop.
$(document).ready(function(){
$('form').on('submit',function(e){
e.preventDefault();
$.ajax({
type:'post',
url:'/rolls'
}).done(function(data){
$('#die').html('<img src=' + data + ".png>");
});
});
});
<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"></div>
</div>
get '/' do
erb :index
end
# TODO: convert this route to use AJAX
post '/rolls' do
# If the user passes-in a "value", let's use it. Otherwise, we'll generate a random one.
# See: roll_if_value_is_nil method in the Roll model.
value = params[:value] ? params[:value].to_i : nil
@roll = value ? Roll.create({ value: value }) : Roll.create
@roll.value.to_s
# erb :index # HINT: what does this do? what should we do instead?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment