安装 gems
gem install sequel -NV
gem install ramaze -NV
gem install maruku -NV
初始化建表 db.rb
require "sequel"
DB = Sequel.sqlite("myblog.db")
DB.create_table(:blog) do
primary_key :id
String :title
Text :content
DateTime :ctime
end
写业务代码 app.rb
require 'ramaze'
require "sequel"
require 'maruku'
DB = Sequel.sqlite("myblog.db")
class BlogController < Ramaze::Controller
engine :haml
map "/blog"
def new
render_view :new
end
end
Ramaze.start
启动
ruby app.rb
建模板目录
md view/blog
发表博客的页面
view/blog/new.haml
!!!
%html
%head
%meta{charset: "utf-8"}
%link{href: "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css",rel: "stylesheet"}
%body
.container
.row
.col-md-12
%form{action: "save", method: "post"}
.form-group
%label{for: "title"} title
%input#t.form-control{name: "title", placeholder: "title", type: "text"}
.form-group
%label{for: "content"} content
%textarea#c.form-control{name: "content", rows: "12"}
:preserve
## markdown ##
- rails
- sinatra
[ goto ](https://www.ruby-china.org)
%button.btn.btn-primary{type: "submit"} Submit
app.rb 中增加写博客保存到数据库和查询博客
def save
request.params[:ctime]=Time.now
DB[:blog].insert request.params
redirect "/blog"
end
def index
@records = DB[:blog].all
render_view :index
end
列出所有的博客的页面
view/blog/index.haml
!!!
%html
%head
%meta{charset: "utf-8"}
%link{href: "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css",rel: "stylesheet"}
%body
.container
%a{href: "blog/new"} New
%hr
.row
.col-md-12
- @records.each do |record|
%br
.card{style: "padding:1rem"}
.card-header
%h1= record[:title]
%i.text-right
published at: #{record[:ctime]}
.card-body
%p
= Maruku.new(record[:content]).to_html
一个自用 blog 基本完成
看看表,不到 5 分钟,比拍黄片快多了