create an empty project and add a Gemfile
cd ~/Desktop
mkdir project-name
cd project-name
touch Gemfile# Gemfile
source 'https://rubygems.org'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'rake'Install the dependencies
bundle installCreate an app.rb file
# app.rb
require 'sinatra'
require 'sinatra/activerecord'
set :database, "sqlite3:project-name.sqlite3"Create a Rakefile
# Rakefile
require 'sinatra/activerecord/rake'
require './app'Create a migration for creating a users table
rake db:create_migration NAME=create_users_tableAdd code to the migration for creating columns
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
endRun the migration
rake db:migrateCreate a User model
# models.rb
class User < ActiveRecord::Base
endLoad the User model into your app
# at the bottom of app.rb
require './models'Create some users with IRB
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())Create an index.erb file in a views directory (views/index.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- Create migration with rake
- Populate the migration with code for adding columns
- Run the migration with rake db:migrate
- Create the model (add class to models file)
- Add some rows to the table with IRB
- Create a route and a view for displaying records
When I do
rake db:create_migration NAME=create_users_tableit throws an errorCan you help me fix it?