2007-05-23

每天一剂Rails良药之Automatically Save a Draft of a Form

关键字: Rails Automatically Save a Draft of a Form
今天我们来看看Gmail里的Ajax自动保存草稿在Rails里的实现

首先在layout里引入Javascript标签
[coce]
<%= javascript_include_tag :defaults %>
[/code]

posts_controller.rb
[code] def new if request.get? @post = session[:post_draft] || Post.new else @post = Post.create(params:post]) session[:post_draft] = nil redirecto_to :action => 'list' end end def save_draft session[:post_draft] = Post.new(params[:post]) render :text => "<i>Draft saved at #{Time.now}</i>" end [/code]

posts/new.rhtml
[code] <% form_for :post, @post, :url => {:action => "new" }, :html => {:id => 'post_form'} do |f| %> Title: <%= f.text_field :title %><br/> Body: <%= f.text_area :body %><br/> <%= submit_tag "Save Post" %> <% end %> <div id='draft-message' style='color:read;'> </div> <%= observe_form 'post_form', :url => {:action => 'save_draft'}, :update => 'draft-message', :frequency => 30 %> [/code]

BTW:以前就有过在蛙眼写博客然后发表时浏览器崩溃的情况,现在都先用文本文件写然后转贴,蛙眼怎么不把这个功能不加上呢?
评论
axgle 2007-06-22
nice!~
发表评论

您还没有登录,请登录后发表评论

hideto
搜索本博客
我的相册
A6bdc31c-c66e-468e-961e-9cc721e82adc-thumb
screenshot
共 1 张
存档
最新评论