Skip to content

Instantly share code, notes, and snippets.

@jtallant
Last active March 25, 2024 13:26
Show Gist options
  • Save jtallant/fd66db19e078809dfe94401a0fc814d2 to your computer and use it in GitHub Desktop.
Save jtallant/fd66db19e078809dfe94401a0fc814d2 to your computer and use it in GitHub Desktop.

Revisions

  1. jtallant revised this gist Dec 6, 2016. 1 changed file with 22 additions and 6 deletions.
    28 changes: 22 additions & 6 deletions setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -86,14 +86,30 @@ Load the User model into your app
    require './models'
    ```

    Create some users with IRB
    Create a seeds file

    ```ruby
    irb
    require './app' # Load app into IRB session
    User.connection
    User
    User.create(fname: 'Jane', lname: 'Doe', email: '[email protected]', created_at: Time.now(), updated_at: Time.now())
    touch db/seeds.rb
    ```

    Write some seeds

    ```ruby
    # db/seeds.rb
    users = [
    {fname: 'Jon', lname: 'Doe', email: '[email protected]'},
    {fname: 'Jane', lname: 'Doe', email: '[email protected]'}
    ]

    users.each do |u|
    User.create(u)
    end
    ```

    Run the seeds

    ```ruby
    rake db:seed
    ```

    Create an index.erb file in a views directory (views/index.erb)
  2. jtallant revised this gist Nov 30, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -120,7 +120,7 @@ Create a route for the home page
    # app.rb
    get '/' do
    @users = User.all
    erb :home
    erb :index
    end
    ```

  3. jtallant revised this gist Nov 30, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -90,6 +90,7 @@ Create some users with IRB

    ```ruby
    irb
    require './app' # Load app into IRB session
    User.connection
    User
    User.create(fname: 'Jane', lname: 'Doe', email: '[email protected]', created_at: Time.now(), updated_at: Time.now())
  4. jtallant revised this gist Nov 30, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -92,7 +92,7 @@ Create some users with IRB
    irb
    User.connection
    User
    User.create(fname: 'Jane', lname: 'Doe', email: '[email protected]', created_at: Time.now(), updated_at: Time.now()))
    User.create(fname: 'Jane', lname: 'Doe', email: '[email protected]', created_at: Time.now(), updated_at: Time.now())
    ```

    Create an index.erb file in a views directory (views/index.erb)
  5. jtallant revised this gist Nov 30, 2016. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -121,4 +121,14 @@ get '/' do
    @users = User.all
    erb :home
    end
    ```
    ```

    #### Adding more tables (models)

    1. Create migration with rake
    1. Populate the migration with code for adding columns
    1. Run the migration with rake db:migrate
    1. Create the model (add class to models file)
    1. Add some rows to the table with IRB
    1. Create a route and a view for displaying records

  6. jtallant revised this gist Nov 30, 2016. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,6 @@ Create a Rakefile

    ```ruby
    # Rakefile

    require 'sinatra/activerecord/rake'
    require './app'
    ```
    @@ -74,15 +73,15 @@ rake db:migrate

    Create a User model

    ```
    ```ruby
    # models.rb
    class User < ActiveRecord::Base
    end
    ```

    Load the User model into your app

    ```
    ```ruby
    # at the bottom of app.rb
    require './models'
    ```
    @@ -116,9 +115,8 @@ Create an index.erb file in a views directory (views/index.erb)

    Create a route for the home page

    ```
    ```ruby
    # app.rb
    get '/' do
    @users = User.all
    erb :home
  7. jtallant created this gist Nov 30, 2016.
    126 changes: 126 additions & 0 deletions setting-up-sinatra-with-active-record.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,126 @@
    # Setting up Sinatra Project

    create an empty project and add a Gemfile

    ```bash
    cd ~/Desktop
    mkdir project-name
    cd project-name
    touch Gemfile
    ```

    ```ruby
    # Gemfile
    source 'https://rubygems.org'

    gem 'activerecord'
    gem 'sinatra-activerecord'
    gem 'sqlite3'
    gem 'rake'
    ```

    Install the dependencies

    ```bash
    bundle install
    ```

    Create an app.rb file

    ```ruby
    # app.rb
    require 'sinatra'
    require 'sinatra/activerecord'

    set :database, "sqlite3:project-name.sqlite3"
    ```

    Create a Rakefile

    ```ruby
    # Rakefile

    require 'sinatra/activerecord/rake'
    require './app'
    ```

    Create a migration for creating a users table

    ```bash
    rake db:create_migration NAME=create_users_table
    ```

    Add code to the migration for creating columns

    ```ruby
    class CreateUsersTable < ActiveRecord::Migration[5.0]
    def change
    create_table :users do |t|
    t.string :fname
    t.string :lname
    t.string :email
    t.datetime :created_at
    t.datetime :updated_at
    end
    end
    end
    ```

    Run the migration

    ```bash
    rake db:migrate
    ```

    Create a User model

    ```
    # models.rb
    class User < ActiveRecord::Base
    end
    ```

    Load the User model into your app

    ```
    # at the bottom of app.rb
    require './models'
    ```

    Create some users with IRB

    ```ruby
    irb
    User.connection
    User
    User.create(fname: 'Jane', lname: 'Doe', email: '[email protected]', created_at: Time.now(), updated_at: Time.now()))
    ```

    Create an index.erb file in a views directory (views/index.erb)

    ```erb
    <!DOCTYPE html>
    <html>
    <head>
    <title>Users</title>
    </head>
    <body>
    <ul>
    <% @users.each do |user| %>
    <li><%= user.email %></li>
    <% end %>
    </ul>
    </body>
    </html>
    ```

    Create a route for the home page

    ```
    # app.rb
    get '/' do
    @users = User.all
    erb :home
    end
    ```