A skeleton web application configured to use Sinatra and ActiveRecord
at master 87 lines 2.4 kB view raw view rendered
1## sinatree 2 3A skeleton web application configured to use Sinatra and ActiveRecord with 4some simple conventions: 5 6- `views` directory set to `app/views/(current controller name)` 7 8- default layout configured to `app/views/layouts/application.erb`, with 9 per-controller layouts `app/views/layouts/(current controller name).erb` 10 used first 11 12- database tables using non-auto-incrementing IDs (see 13[`UniqueId`](https://github.com/jcs/sinatree/blob/master/lib/unique_id.rb)) 14 15### Usage 16 17Clone `sinatree`: 18 19 $ git clone https://github.com/jcs/sinatree.git 20 $ cd sinatree 21 22Then install Bundler dependencies: 23 24 sinatree$ bundle config set --local path vendor/bundle 25 sinatree$ bundle install 26 27Initialize a session secret key: 28 29 sinatree$ ruby -e 'require "securerandom"; print SecureRandom.hex(64)' > config/session_secret 30 31To create a database table `users` for a new `User` model: 32 33 sinatree$ $EDITOR `bundle exec rake db:create_migration NAME=create_user_model` 34 35 class CreateUserModel < ActiveRecord::Migration[5.2] 36 def change 37 create_table :users do |t| 38 t.timestamps 39 t.string :username 40 t.string :password_digest 41 end 42 end 43 end 44 45Then run the database migrations: 46 47 sinatree$ bundle exec rake db:migrate 48 49The new `User` model can be created as `app/models/user.rb`: 50 51 class User < DBModel 52 has_secure_password 53 end 54 55A root controller can be created as `app/controllers/home_controller.rb`: 56 57 class HomeController < ApplicationController 58 self.path = :root 59 60 get "/" do 61 "Hello, world" 62 end 63 end 64 65To run a web server with your application: 66 67 sinatree$ bin/server 68 69To access an IRB console: 70 71 sinatree$ bin/console 72 73### License 74 75Copyright (c) 2017-2025 joshua stein `<jcs@jcs.org>` 76 77Permission to use, copy, modify, and distribute this software for any 78purpose with or without fee is hereby granted, provided that the above 79copyright notice and this permission notice appear in all copies. 80 81THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 82WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 83MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 84ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 85WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 86ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 87OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.