Reloj
A lightweight web framework for Ruby for creating database-backed web applications with the Model-View-Controller pattern. It features an ActiveRecord pattern ORM, convenient methods for cookie storage for things like session tokens, convenient executables to make development faster (like reloj new name_of_app
), and is designed to be easy to deply. Check out a live app built with reloj and deployed on heroku
Getting Started
Install Reloj at the command prompt if you haven't yet:
gem install reloj
At the command prompt, create a new Reloj application:
reloj new myapp
where "myapp" is the application name.
Change directory to
myapp
and start the web server:cd myapp reloj server
Using a browser, go to
http://localhost:3000
and you'll see: "Welcome to Reloj!"
Setting up the database
Reloj has some built in convenience features to make it easy to set up your database
To name your database, change the dbname value in config/db.yml
. If you need to setup any other values for your local database (username, password, port), you can put them in the same file.
To create a database for your app to use, just run:
bundle exec rake db:create
To destroy the database, run:
bundle exec rake db:delete
To reset the database, run:
bundle exec rake db:reset
Finally, to setup your schema and seed your database, use the db/setup.sql
file. To run this file on the app's databasae, run:
bundle exec rake db:setup
To learn how to manage the database when deploying, scroll down to the Deploy secction
Models and ORM
Reloj uses the active record pattern for its object-relational mapping.
To use this functionality in your app, create a class for your model in app/models
and have the model inherit from ModelBase
class Cat < ModelBase
# custom code goes here
finalize!
end
Some commands:
Cat.all
- Returns an array with instances of class Cat, one instance for each row in table cats
Cat.find(2)
- Finds the record in table cats with id 2, returns instance of Cat corresponding to that record
Controllers
Create controllers in app/controllers
. Controllers should inherit from ControllerBase
.
class CatsController < ControllerBase
def index
@cats = Cat.all
render :index
end
end
Routes
Write your routes in config/routes.rb
module App
ROUTES = Proc.new do
get '/cats', CatsController, :index
get '/cats/new', CatsController, :new
post '/cats', CatsController, :create
end
end
Running the sample app
Reloj includes a generator for a sample app. To check it out:
Generate the sample app
reloj generate:sample
Move into the sample app directory
cd reloj_sample
Run the server
reloj server
Navigate to localhost:3000
Deploying
Reloj is built to make deploying to heroku as easy as possible, here's how:
Make sure there's a procfile in the project's root directory with the following line:
web: bundle exec reloj server
Commit your changes to git
git commit -am "procfile"
Create a new heroku app using the heroku cli
heroku apps:create mynewapp
Push your code to heroku (heroku apps:create should automatically add a remote repo to push to)
git push heroku master
Wait for the push to finish, then create your database tables and seed it as defined in
db/setup.sql
heroku run rake db:setup
Wait for the rake task to finish, then open your app:
heroku open
Enjoy your now-deployed app!
TODO
- WEBrick doesn't support PATCH/DELETE. Maybe I can monkey-patch that in using servlets
- Right now it doesn't serve assets. I think I can get it that work using servlets, also.
- REFERENCE: http://ruby-doc.org/stdlib-1.9.3/libdoc/webrick/rdoc/WEBrick.html#module-WEBrick-label-Servlets