Skip to content

Instantly share code, notes, and snippets.

@KentCarmine
Forked from dbc-challenges/lucky_ajax.md
Last active December 27, 2015 00:49
Show Gist options
  • Save KentCarmine/7240939 to your computer and use it in GitHub Desktop.
Save KentCarmine/7240939 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
$("form").submit(function(event){
event.preventDefault();
var roll_result = Math.floor((Math.random()*6)+1);
$.post("/", roll_result, function(response){
var die_img_result = $(response).find("img");
$('#die').empty();
$('#die').append(die_img_result);
});
});
});
get '/' do
erb :index
end
# TODO: convert this route to use AJAX
post '/' 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
erb :index # HINT: what does this do? what should we do instead?
end

Instructions:

  1. Download this application skeleton.
  2. Convert the app to use AJAX.
  3. Add any files you changed to your gist and submit your code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment