Sinatra, MySQL and ActiveRecord

后端 未结 2 1068
天涯浪人
天涯浪人 2021-02-03 16:18

how do I set up a simple sinatra app to use MySQL and ActiveRecord? I found some solutions, but none of them worked (maybe they are outdated):

http://ericfarkas.com/post

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 16:21

    You can still use sinatra-activerecord gem with MySQL and avial other features too.

    Following is the way to use it:

    require "sinatra/activerecord"
    
    set :database, "mysql2://#{db_username}:#{db_password}@#{db_host}:#{db_port}/#{db_database}"
    
    # Model
    class User < ActiveRecord::Base
    end 
    
    get '/' do
      @items = User.all
      erb :index
    end
    

提交回复
热议问题