雷神岛刷咕咕:sinatra+sequel创建只需要一个文件的blog程序

来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 20:25:33
2010.04.10更新:完整的CRUD
为了能在论坛里可见,改用haml展示代码。partial也很简单,事实上,直接使用haml和erb就是render partial。

Rails最经典的是15分钟创建blog,充分体现了rails开发的迅速。于是我使用sequel作为O/RMapping也作了一个相同的应用,由于使用的是sinatra,而没有使用其他的辅助库,我写routes浪费了半天时间。为了程序下载方便,我使用了inline模板,没有高亮和缩进支持,浪费了一些时间。Sequel的工作方式和ActiveRecord不一样,总共花了将近一个半小时。有兴趣的可以参考sinatra作者的blog程序,scanty。

Ruby代码  
  1. # run gem install sinatra sequel haml amalgalite first  
  2. require 'rubygems'  
  3. require 'sinatra'  
  4. require 'sequel'  
  5. require 'haml'  
  6.   
  7. helpers do  
  8.   def h(string)  
  9.     string.to_s.gsub('<','<').  
  10.       gsub('>','>').gsub('"', '"')  
  11.   end  
  12. end  
  13.   
  14. configure do  
  15.   DB = Sequel.connect('amalgalite://notes.db')  
  16.   DB.create_table?(:notes) do  
  17.     primary_key :id  
  18.     text :title  
  19.     text :body  
  20.   end  
  21.   class Note < Sequel::Model  
  22.   end  
  23. end  
  24.   
  25. get '/notes' do  
  26.   @notes = Note.all  
  27.   haml :index  
  28. end  
  29.   
  30. get '/notes/:id' do |id|  
  31.   pass if id == 'new'  
  32.   @note = Note[:id => id]  
  33.   haml(:show) + haml(:back_to_top)  
  34. end  
  35.   
  36. get '/notes/new' do  
  37.   @note = Note.new  
  38.   haml(:new)+haml(:back_to_top)  
  39. end  
  40.   
  41. get '/notes/:id/edit' do |id|  
  42.   @note = Note[:id => id]  
  43.   haml(:edit) +  
  44.     haml(:back_to_top)  
  45. end  
  46.   
  47. put '/notes/:id' do |id|  
  48.   note = Note.find(:id => id)  
  49.   note.update(:title => params[:title], :body => params[:body])  
  50.   note.save  
  51.   redirect "/notes/#{id}"  
  52. end  
  53.   
  54. post '/notes' do  
  55.   note = Note.new(:title => params[:title], :body => params[:body])  
  56.   note.save  
  57.   redirect "/notes"  
  58. end  
  59.   
  60. delete '/notes/:id' do |id|  
  61.   note = Note.find(:id => id)  
  62.   note.destroy  
  63.   redirect "/notes"  
  64. end  
  65.   
  66. __END__  
  67. @@ layout  
  68. %html  
  69.   %head  
  70.     %title note in sinatra  
  71.   %body= yield  
  72. @@ index  
  73. %div.body  
  74.   %table{:width => "100%", :style => "border-collapse: collapse", :border => 1}  
  75.     %thead  
  76.       %tr   
  77.         %td title  
  78.         %td    
  79.         %td    
  80.     %tbody  
  81.       - @notes.each do |note|  
  82.         %tr  
  83.           %td= h note.title  
  84.           %td   
  85.             %a{:href => "/notes/#{note.id}"} show  
  86.           %td  
  87.             %a{:href => "/notes/#{note.id}/edit"} edit  
  88.   %a{:href => '/notes/new'} new note  
  89. @@ show  
  90. %h3= h @note[:title]  
  91. %div= @note[:body]  
  92. @@ edit  
  93. %form{:action => "/notes/#{@note.id}", :method => "post"}  
  94.   %input{ :type => "hidden", :name => "_method", :value => "put"}  
  95.   = haml :form  
  96. %form{:action => "/notes/#{@note.id}", :method => "post"}  
  97.   %input{ :type => "hidden", :name => "_method", :value => "delete"}  
  98.   %input{ :type => "submit", :value => 'delete'}/  
  99. @@ new  
  100. %form{:action => "/notes", :method => "post"}  
  101.   = haml :form  
  102. @@ form  
  103. %label{:for=>"title"} Description  
  104. %br  
  105. %input{:name=>"title", :value=>h(@note.title), :size => 50}  
  106. %br  
  107. %label{:for => "body"}Content  
  108. %br  
  109. %textarea{:name => "body", :cols => 30, :rows => 10}= @note.body  
  110. %br  
  111. %input{:type => "submit"}  
  112. @@ back_to_top  
  113. %div  
  114.   %a{ :href => "/notes"} Back to Top  


里面有helper,partial的sinatra版。
configure do..end 代码段是为了在写程序的时候sinatra可以正确的reload而不显示警告。