请您先登录,才能继续操作
[ruby] sinatra+sequel创建只需要一个文件的blog程序
花花公子
2009-02-24
2010.04.10更新:完整的CRUD
为了能在论坛里可见,改用haml展示代码。partial也很简单,事实上,直接使用haml和erb就是render partial。 Rails最经典的是15分钟创建blog,充分体现了rails开发的迅速。于是我使用sequel作为O/R Mapping也作了一个相同的应用,由于使用的是sinatra,而没有使用其他的辅助库,我写routes浪费了半天时间。为了程序下载方便,我使用了inline模板,没有高亮和缩进支持,浪费了一些时间。Sequel的工作方式和ActiveRecord不一样,总共花了将近一个半小时。有兴趣的可以参考sinatra作者的blog程序,scanty。 # run gem install sinatra sequel haml amalgalite first require 'rubygems' require 'sinatra' require 'sequel' require 'haml' helpers do def h(string) string.to_s.gsub('<','<'). gsub('>','>').gsub('"', '"') end end configure do DB = Sequel.connect('amalgalite://notes.db') DB.create_table?(:notes) do primary_key :id text :title text :body end class Note < Sequel::Model end end get '/notes' do @notes = Note.all haml :index end get '/notes/:id' do |id| pass if id == 'new' @note = Note[:id => id] haml(:show) + haml(:back_to_top) end get '/notes/new' do @note = Note.new haml(:new)+haml(:back_to_top) end get '/notes/:id/edit' do |id| @note = Note[:id => id] haml(:edit) + haml(:back_to_top) end put '/notes/:id' do |id| note = Note.find(:id => id) note.update(:title => params[:title], :body => params[:body]) note.save redirect "/notes/#{id}" end post '/notes' do note = Note.new(:title => params[:title], :body => params[:body]) note.save redirect "/notes" end delete '/notes/:id' do |id| note = Note.find(:id => id) note.destroy redirect "/notes" end __END__ @@ layout %html %head %title note in sinatra %body= yield @@ index %div.body %table{:width => "100%", :style => "border-collapse: collapse", :border => 1} %thead %tr %td title %td %td %tbody - @notes.each do |note| %tr %td= h note.title %td %a{:href => "/notes/#{note.id}"} show %td %a{:href => "/notes/#{note.id}/edit"} edit %a{:href => '/notes/new'} new note @@ show %h3= h @note[:title] %div= @note[:body] @@ edit %form{:action => "/notes/#{@note.id}", :method => "post"} %input{ :type => "hidden", :name => "_method", :value => "put"} = haml :form %form{:action => "/notes/#{@note.id}", :method => "post"} %input{ :type => "hidden", :name => "_method", :value => "delete"} %input{ :type => "submit", :value => 'delete'}/ @@ new %form{:action => "/notes", :method => "post"} = haml :form @@ form %label{:for=>"title"} Description %br %input{:name=>"title", :value=>h(@note.title), :size => 50} %br %label{:for => "body"}Content %br %textarea{:name => "body", :cols => 30, :rows => 10}= @note.body %br %input{:type => "submit"} @@ back_to_top %div %a{ :href => "/notes"} Back to Top 里面有helper,partial的sinatra版。 configure do..end 代码段是为了在写程序的时候sinatra可以正确的reload而不显示警告。 |
|
rainly
2009-06-10
环境: sequel-3.1.0 sinatra-0.9.2
无法正常运行,报错信息如下: blogs.rb:20: undefined method `table_exists?' for Blog:Class |
|
花花公子
2009-06-10
--- a/sinatra+sequel/notes.rb
+++ b/sinatra+sequel/notes.rb @@ -13,6 +13,7 @@ configure do Sequel.connect('sqlite://notes.db') class Note < Sequel::Model + plugin :schema |
|
rainly
2009-06-11
加入修正后,报错信息如下:
no such file to load -- haml file: custom_require.rb location: gem_original_require line: 31 blogs.rb in GET /blogs/new |
|
rainly
2009-06-11
OK,原来是少了haml插件,安装上即可
sudo gem install haml |
|
koalant
2009-06-23
我最近才刚刚看 sinatra , 感觉用来做 web api 应用非常合适。
sinatra 对 rack 的依赖比较多,很多都是继承自 rack 中的类。 你的那个 helper 方法 h 现在可以完全不用自己来写。 helpers do include Rack:Utils alias_method :h, :escape_html end |
|
ray_linn
2009-11-10
这个15分钟。。。。。的东西简直太忽悠人了。。
|