sinatra mongodb - rabbit slide show¨mongodb 今回はs inatraでm ongodbの 操作...
TRANSCRIPT
HTTP MethodこれらのGETはHTTP Methodの一部で、主にGETやPOSTが使われています
GET POST PUT DELETE
OPTION
何か見せる
何か生成する
何か更新する
何か削除する
何か満たす
http://www.sinatrarb.com/intro-ja.html より引用
POST
require 'sinatra'
post '/' do "Hello world!"end
get '/' do erb <<EOF <form method='post' action='/'> <input type='submit' /> </form>EOFend
MongoDBまずは基本から。
$ mongo # クライアントの起動> db # 現在のテーブル> use foo # foo をこれから使用(この時点ではまだ書き込まれていない)> db # foo
MongoDBまずは基本から。
> a = { name: 'Frank' }> db.sinatraDB.insert(a) # 挿入> show collections # sinatraDBが書き込まれている> db.sinatraDB.find() # 検索
MongoDB
require 'sinatra'require 'mongo'
include Mongo # Mongoの省略
configure do conn = MongoClient.new # localhost:27017 set connection: conn set db: conn.db('foo') # use fooend
get '/collections' do settings.db.collection_names.to_a.to_s # show collections と同じend
MongoDB
require 'sinatra'require 'mongo'
get '/' do # ドキュメント全体 settings.db['sinatraDB'].find.to_a.to_send
get '/:collection' do |col| # name が collectionであるものをひとつ返す settings.db['sinatraDB'].find_one(name: col).to_send
Haml./views/index.haml
%form{action: '/', method: 'post'} %input{name: 'name', placeholder: 'name'} %input{type: 'submit'}
注意:インデント幅はソフトスペース”2”です
MongoDBSinatraからデータを登録します
post '/' do bson_id = settings.db['sinatraDB'].insert(params) bson_id.to_send
このIDをコピーしてください
MongoDBBSON_IDからの検索
get '/id/:id' do |id| id = BSON::ObjectId.from_string(id) settings.db['sinatraDB'].find(id).to_send
ヘルパー
helpers do def find_by_name(name) settings.db['sinatraDB'].find(name: name) end
def find_all settings.db['sinatraDB'].find endend
データの更新
./views/index.haml
%ul - find_all.each do |document| %li %a{href: "/update/#{document['_id']}"}= document['name']
データの更新
./views/update.haml
%h1= find_by_id(@id)
%form{action: "/update/#{@id}", method: 'post'} %input{name: 'name', value: find_by_id(@id)} %input{type: 'submit'}
データの更新
ヘルパー
helpers do def find_by_id(id) id = BSON::ObjectId.from_string(id) settings.db['sinatraDB'].find_one(id)['name'] endend
データの更新
post '/update/:id' do |id| id = BSON::ObjectId.from_string(id) settings.db['sinatraDB'].update({_id: id}, params) redirect '/'end
データの削除
./views/index.haml
%ul - find_all.each do |document| %li = document['name'] | %a{href: "/update/#{document['_id']}"} 更新 | %a{href: "/delete/#{document['_id']}"} 削除
データの削除
ルーティング
get '/delete/:id' do |id| id = BSON::ObjectId.from_string(id) settings.db['sinatraDB'].remove(_id: id) redirect '/'end
本来はPOSTを使うのですが、今回は操作感を重視してGETを使います
データの追加
はじめに追加した部分もredirectに書き換えましょう
post '/' do settings.db['sinatraDB'].insert(params) redirect '/'end
未完了と完了
./views/index.haml
%ul - find_all.each do |document| %li - if document['status'] %s= document['name'] | %a{href: "/delete/#{document['_id']}"} 削除 - else = document['name'] | %a{href: "/done/#{document['_id']}"} 完了 | %a{href: "/update/#{document['_id']}"} 更新 | %a{href: "/delete/#{document['_id']}"} 削除
未完了と完了
ルーティング
get '/done/:id' do |id| id = BSON::ObjectId.from_string(id) settings.db['sinatraDB'].update( {_id: id}, { "$set" => {status: Time.now} } ) redirect '/'end