雷神岛刷咕咕: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代码![](http://image35.360doc.cn/DownloadImg/2011/08/2821/16625982_1.png)
里面有helper,partial的sinatra版。
configure do..end 代码段是为了在写程序的时候sinatra可以正确的reload而不显示警告。
为了能在论坛里可见,改用haml展示代码。partial也很简单,事实上,直接使用haml和erb就是render partial。
Rails最经典的是15分钟创建blog,充分体现了rails开发的迅速。于是我使用sequel作为O/RMapping也作了一个相同的应用,由于使用的是sinatra,而没有使用其他的辅助库,我写routes浪费了半天时间。为了程序下载方便,我使用了inline模板,没有高亮和缩进支持,浪费了一些时间。Sequel的工作方式和ActiveRecord不一样,总共花了将近一个半小时。有兴趣的可以参考sinatra作者的blog程序,scanty。
Ruby代码
![](http://image35.360doc.cn/DownloadImg/2011/08/2821/16625982_1.png)
- # 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而不显示警告。
sinatra+sequel创建只需要一个文件的blog程序
创建一个简单的openlaszlo程序
爱情 只需要 一个张开的怀抱
解决vs2008生成的程序安装文件需要安装.net framework 3.5的问题
批处理bat文件实现复制、删除、创建文件夹、执行程序、打开文件
MATLAB-数据处理-一个文件数据处理小程序
Native程序的sources文件
幸福有时候只需要一个
程序文件
转载【火风凰的博客】------ 幸福有时候只需要一个台阶
改变文件和程序的关联方法
怎样创建sitemap文件
linux创建指定大小的文件的方法
XP提高网速只需要一个协议-xp的一个秘密武器
XP提高网速只需要一个协议——xp的一个秘密武器
在VC中创建DLL文件的方法步骤
在Windows下创建.htaccess文件的方法
Linux中没有文件创建时间的概念
幸福有时候只需要一个台阶..
分享:幸福有时候只需要一个台阶
幸福有时就只需要一个台阶
幸福有时候只需要一个台阶
幸福,有时候只需要一个台阶
有时候幸福只需要一个台阶