Skip to content

Instantly share code, notes, and snippets.

@mariapacana
Forked from dbc-challenges/lucky_ajax.md
Last active December 18, 2015 02:08
Show Gist options
  • Save mariapacana/5708317 to your computer and use it in GitHub Desktop.
Save mariapacana/5708317 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
$('form').submit(function (e) {
e.preventDefault();
$.post('/', function(response) {
source = response + '.png';
$('#die_pic').attr('src', source);
});
});
});
get '/' do
erb :index
end
post '/' do
value = params[:value] ? params[:value].to_i : nil
@roll = value ? Roll.create({ value: value }) : Roll.create
@roll.value.to_s
end
<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="/">
<input type="submit" value="Roll the Die">
</form>
<div id="die">
<img id = "die_pic">
</div>
</div>
# The below was the only line that I changed in this file.
require_relative 'environment'
# Log queries to STDOUT in development
if Sinatra::Application.development?
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
# Automatically load every file in APP_ROOT/app/models/*.rb, e.g.,
# autoload "Person", 'app/models/person.rb'
#
# See http://www.rubyinside.com/ruby-techniques-revealed-autoload-1652.html
#
Dir[APP_ROOT.join('app', 'models', '*.rb')].each do |model_file|
filename = File.basename(model_file).gsub('.rb', '')
autoload ActiveSupport::Inflector.camelize(filename), model_file
end
# We have to do this in case we have models that inherit from each other.
# If model Student inherits from model Person and app/models/student.rb is
# required first, it will throw an error saying "Person" is undefined.
#
# With this lazy-loading technique, Ruby will try to load app/models/person.rb
# the first time it sees "Person" and will only throw an exception if
# that file doesn't define the Person class.
# Heroku controls what database we connect to by setting the DATABASE_URL environment variable
# We need to respect that if we want our Sinatra apps to run on Heroku without modification
db = URI.parse(ENV['DATABASE_URL'] || "postgres://localhost/#{APP_NAME}_#{Sinatra::Application.environment}")
DB_NAME = db.path[1..-1]
# Note:
# Sinatra::Application.environment is set to the value of ENV['RACK_ENV']
# if ENV['RACK_ENV'] is set. If ENV['RACK_ENV'] is not set, it defaults
# to :development
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:port => db.port,
:username => db.user,
:password => db.password,
:database => DB_NAME,
:encoding => 'utf8'
)
# Below is the only line that I changed in this file.
require_relative '../../config/environment'
class Roll < ActiveRecord::Base
attr_accessible :value
validates :value, :inclusion => { :in => (1..6), :message => "must be between 1 and 6" }
after_initialize :roll_if_value_is_nil
private
# If the user passes-in a "value", let's use it. Otherwise, we'll generate a random one.
def roll_if_value_is_nil
self.value = (rand(6) + 1) if not self.value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment