SinatraとSQLiteと
はじめに
相変わらずいろいろ迷走してますが、簡易的なブログを作ってみることにしました。
Sinatraを使って。
まずはテストとして、以前作成したひな形に必要な機能を追加していくことにします。
ファイル構成
構成はこんな感じに。
プロジェクトのルートディレクトリ Lapp.rb Lconfig.ru LGemfile LViews Llayout.slim Ltop.slim Labout.slim Lcss Lstyle.sass Ljs Lscript.coffee ---------------- 追加したファイル ---------------- Ldb Lblogcontents.db --- DB(今回はSQLite3) Ldatabase.yml --- 使用するDBの種類(今回はSQLite3)や、アクセスするDBのファイルのパスを指定している。 Lschema.rb --- migrate実行時(テーブル作成時)に自動で作成されるファイル。実行した操作の確認ができる。 Lmigrate LXXX_create_blogcontents.rb --- DBにテーブルを追加するmigrateファイルで、XXXの部分はファイル作成時の日付。 LRakefile --- rakeコマンドを実行するためのファイル。今回はDBのテーブル作成を行うmigrateのために追加。 Lmodels Lblogcontents.rb --- DBにアクセスするためのクラス。 Lvendor Lbundle L... --- bundle install/updateでインストールされるgemファイル。
Gemファイルをプロジェクト内にインストールする
バージョンを固定しておきたい場合など、プロジェクトごとにGemファイルをインストールしておくほうが便利。
というわけで、プロジェクトのvendor/bundleにインストールしてみます。
Gemファイルなどは変更なし。bundle installを以下のように変更します。
bundle install --path vendor/bundle
以降rackを使うコマンドを、[bundle exec 〜]に置き換える必要があります。
このあと[rackup]を変更せずに実行していて、ファイルが見つからないとエラーになってしばらくハマりました...。
[bundle exec rackup]とすることで、vendor/bundleにインストールしたファイルを使用して実行できます。
DBのテーブル作成
DBとのやりとりはRailsで使用されていることからも、ActiveRecordを使ってSQLiteに接続してみることにしました。
(DBはPostgreSQLなどに変更するかもしれません)
まずはテーブルの作成から。
rakeコマンドを実行するために、プロジェクト直下にRakefileという名前でファイルを作成します。
Rakefile
require 'sinatra/activerecord' require 'sinatra/activerecord/rake'
これでコマンドが使用可能になるわけですが、どんなものが使えるかというと...
bundle exec rake -T rake db:create rake db:create_migration rake db:drop rake db:fixtures:load rake db:migrate rake db:migrate:status rake db:rollback rake db:schema:cache:clear rake db:schema:cache:dump rake db:schema:dump rake db:schema:load rake db:seed rake db:setup rake db:structure:dump rake db:structure:load rake db:version
migration
テーブルを作成するために、マイグレーションファイルを作成します。
bundle exec rake db:create_migration NAME=create_blogcontents
これで20141229053646_create_blogcontents.rbのようなファイルが、db/migrateに作成されます。
この内容を、テーブルを作成するよう変更します。
20141229053646_create_blogcontents.rb
class CreateBlogcontents < ActiveRecord::Migration def change create_table :blogcontents do |tblcontents| tblcontents.string :contentsid tblcontents.string :title tblcontents.string :contents tblcontents.timestamps end end end
で、マイグレーションを実行するために、更に以下のファイルを作成します。 * models/blogcontents.rb * db/database.yml
Rakefileにblogcontents.rbのrequireを追加します。
Rakefile
require 'sinatra/activerecord' require 'sinatra/activerecord/rake' require './models/blogcontents'
追加したファイルの内容は以下の通りです。
database.yml
development: adapter: sqlite3 database: db/blogcontents.db
blogcontents.rb
require 'sqlite3' require 'sinatra/activerecord' ActiveRecord::Base.configurations = YAML.load_file('db/database.yml') ActiveRecord::Base.establish_connection(:development) class Blogcontent < ActiveRecord::Base end
これができたら、マイグレーションを実行します。
bundle exec rake db:migrate
エラーが発生せず、db/blogcontents.dbというファイルが作成されればOK。
※blogcontents.rbでDBファイルのパス設定などが間違っている場合(Base.configurationsやBase.establish_connectionの辺り)、以下のようなエラーが発生しました。
ググるとファイルのアクセス権が〜と言われたりしますが、同じ問題にぶつかった方は一度チェックしてみると良いかもしれません。
rake aborted! SQLite3::CantOpenException: unable to open database file
※Base.establish_connectionの引数を"development"とすると実行は可能ですが、その書き方はDeprecatedになった旨のアラートが表示されます。
ターミナルや、Firefoxのアドオン[SQLite Manager]などを使って、適当にデータを追加します。
ページ1に表示してみる
とりあえずDBを全件検索して、ページ1に表示します。
app.rb
require 'sinatra' require 'sinatra/base' require 'slim' require './models/blogcontents' require 'sinatra/reloader' class MainApp < Sinatra::Base blcContents = Blogcontent.new get '/' do blcContents.getContents @aryContents = Blogcontent.all slim :top end end
top.slim
table thread tr th ID th 題名 th 本文 tbody - @aryContents.each do |content| tr td = content.contentsid td = content.title td = content.contents
これで[bundle exec rackup]してページのトップに遷移すれば、DBに追加したデータが表示されます。