使用sinatra实现简单的crud功能

Sinatra

1
gem install sinatra
  • hello_sinatra
1
2
mkdir -p hello_sinatra
cd hello_sinatra

vim hello_sinatra.rb

1
2
3
4
require "sinatra"
get "/" do
"Hello, world!"
end

然后

ruby hello_sinatra.rb

localhost:4567

修改hello_sinatra.rb

1
2
3
4
require "sinatra"
get "/post/:id" do
"Hello, world!参数是#{params}"
end

然后刷新页面,会发现,有报错,不生效。

Note that every time we change the Ruby file, we’ll need to restart the server

这样每次修改一下,不是很讨厌吗??
shotgun可以避免这个问题。

1
gem install shotgun

然后启动的时候注意一下。

1
ruby hello_sinatra.rb -> shotgun hello_sinatra.rb

这个时候的端口,由4567改变成了9393

Creating a Basic Web App With Ruby and Sinatra

  • build sinatra app
1
2
mkdir pubic-bookmarks-sinatra
cd pubic-bookmarks-sinatra
vim public-bookmarks.rb
1
2
3
4
require 'sinatra'
get '/' do
"#{['Hello', 'Hi', 'Hey', 'Yo'][rand(4)]} World!"
end
vim Gemfile
1
2
3
source 'https://rubygems.org' #gem源自行修改
gem 'sinatra'
ruby '2.1.2'

Note: bundle

vim config.ru
1
2
require './public-bookmarks'
run Sinatra::Application
1
rackup -p 4567
  • layout
1
2
3
4
5
# public-bookmarks.rb
require 'sinatra'
get '/' do
erb :hello_world
end
1
mkdir -p views
1
2
#vim views/hello_world.erb
<%= ['Hello', 'Hi', 'Hey', 'Yo'][rand(4)] %> World!
layout
1
2
3
4
5
6
7
8
9
<!DOCTYPE html>
<html>
<head><title>PublicBookmarksSinatra</title></head>
<body>
<%= yield %>
</body>
</html>
  • Use ActiveRecord
1
2
3
4
# vim Gemfile
gem 'sinatra-activerecord'
gem 'pg'
gem 'rake'

Note: bundle

  • config/database.yml
1
2
3
4
5
6
7
#vim database.yml
development:
adapter: postgresql
database: public-bookmarks-sinatra_development
host: localhost
username: postgres
password: root
1
2
3
# vim Rakefile
require 'sinatra/activerecord/rake'
require './public-bookmarks'
然后在主文件里引入
1
2
3
4
5
6
7
8
# public-bookmarks.rb
require 'sinatra'
require 'sinatra/activerecord'
get '/' do
db_time = database.connection.execute('SELECT CURRENT_TIMESTAMP').first['now']
request.logger.info "DB time is #{db_time}"
erb :hello_world
end
现在重新启动下试试
1
2
rake db:create
rackup -p 4567
可以在后台日志看见相关的输出信息。
  • Build model
1
2
# 首先指定并创建一个migration的名字为create_public_bookmarks
rake db:create_migration NAME=create_public_bookmarks
然后打开这个文件 并且写入相关字段
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# db/migrate/YYYYMMDDHHMMSS_create_public_bookmarks.rb
class CreatePublicBookmarks < ActiveRecord::Migration
def change
create_table :public_bookmarks do |t|
t.string :title
t.string :url
t.text :description
t.string :submitter_email
t.timestamps
end
add_index :public_bookmarks, :url, unique: true
end
end
执行迁移文件
1
rake db:migrate
接下来创建 public_bookmark model
1
mkdir models
1
2
3
# vim models/public_bookmark.rb
class PublicBookmark < ActiveRecord::Base
end
接下来我们需要在主文件里指定model
1
2
3
4
#public-bookmarks.rb
...
require './models/public_bookmark'
...
  • Build Controllers

Note: 在一些时候,我们需要类似rails的notice等闪存方式来提示信息。所以需要安装一个gem.

1
2
3
4
# Gemfile
...
gem 'rack-flash3'
...

Note: bundle

这个和model类似,需要在主文件内部指定实用。
1
2
3
4
5
6
7
8
# public-bookmarks.rb
...
require './models/public_bookmark'
require 'rack-flash'
enable :sessions
use Rack::Flash
...
基本的配置差不多就这样,接下来,可以给controller填充一些action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
get '/public_bookmarks' do
@public_bookmarks = PublicBookmark.all
erb :'public_bookmarks/index'
end
get '/public_bookmarks/new' do
@public_bookmark = PublicBookmark.new
erb :'public_bookmarks/new'
end
get '/public_bookmarks/:id' do
@public_bookmark = PublicBookmark.find(params[:id])
erb :'public_bookmarks/show'
end
post '/public_bookmarks/create' do
@public_bookmark = PublicBookmark.new(params[:public_bookmark])
if @public_bookmark.save
flash[:notice] = 'Public bookmark successfully created!'
redirect to("public_bookmarks/#{@public_bookmark.id}")
else
erb :'public_bookmarks/new'
end
end
然后就可以在views下创建一个public_bookmarks文件夹,创建对应的view.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#views/public_bookmarks/index.erb:
<span>Listing public_bookmarks</span>
<p id="notice"><%= flash[:notice] %></p>
<table>
<thead>
<tr>
<th>Title</th>
<th>Url</th>
<th>Description</th>
<th>Submitter email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @public_bookmarks.each do |public_bookmark| %>
<tr>
<td><%= public_bookmark.title %></td>
<td><%= public_bookmark.url %></td>
<td><%= public_bookmark.description %></td>
<td><%= public_bookmark.submitter_email %></td>
<td>
<a href="/public_bookmarks/<%= public_bookmark.id %>">
Show
</a>
</td>
<% if @authenticated %>
<td>
<form action="/public_bookmarks/destroy/<%= public_bookmark.id %>" method='post'>
<input type='submit' value='Destroy' onclick="return confirm('Are you sure?')">
</form>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<a href='/public_bookmarks/new'>
New Public Bookmark
</a>
#views/public_bookmarks/show.erb:
<p id="notice"><%= flash[:notice] %></p>
<p>
<strong>Title:</strong>
<%= @public_bookmark.title %>
</p>
<p>
<strong>Url:</strong>
<%= @public_bookmark.url %>
</p>
<p>
<strong>Description:</strong>
<%= @public_bookmark.description %>
</p>
<p>
<strong>Submitter email:</strong>
<%= @public_bookmark.submitter_email %>
</p>
<a href='/public_bookmarks'>
Back
</a>
# views/public_bookmarks/new.erb:
<span>New public_bookmark</span>
<%= erb :'public_bookmarks/form' %>
<a href='/public_bookmarks'>
Back
</a>
# views/public_bookmarks/form.erb:
<form action="/public_bookmarks/create" method='post'>
<% if @public_bookmark.errors.any? %>
<div id="error_explanation">
<span><%= pluralize(@public_bookmark.errors.count, "error") %> prohibited this public_bookmark from being saved:</span>
<ul>
<% @public_bookmark.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<label for="public_bookmark_title">Title</label><br>
<input id="public_bookmark_title" name="public_bookmark[title]" type="text">
</div>
<div class="field">
<label for="public_bookmark_url">Url</label><br>
<input id="public_bookmark_url" name="public_bookmark[url]" type="text">
</div>
<div class="field">
<label for="public_bookmark_description">Description</label><br>
<textarea id="public_bookmark_description" name="public_bookmark[description]"></textarea>
</div>
<div class="field">
<label for="public_bookmark_submitter_email">Submitter email</label><br>
<input id="public_bookmark_submitter_email" name="public_bookmark[submitter_email]" type="text">
</div>
<div class="actions">
<input type="submit" value="Create Public bookmark">
</div>
</form>

原文

github

blog原文