`
hideto
  • 浏览: 2690620 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Ruby
阅读更多
今天我们来看看Gmail里的Ajax自动保存草稿在Rails里的实现

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


posts_controller.rb
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


posts/new.rhtml
<% 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 %>


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

相关推荐

    rails 5 test prescriptions build a healthy codebase

    Test the component parts of a Rails application, including the back-end model logic and the front-end display logic. With Rails examples, use testing to enable your code to respond better to future ...

    Ruby-SimpleForm轻松处理Rails表单

    在Ruby on Rails框架中,构建表单是一项常见的任务,而Simple Form gem就是为了简化这一过程而设计的。Simple Form是一款强大的表单构建器,它提供了一种简洁、直观的方式来创建复杂的表单,使得开发者能更专注于...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    With this fully revised new edition, take a holistic view of full-stack development to create usable, high-performing applications with Rails 5. Rails is a great tool for building web applications, ...

    Agile Web Development with Rails 4

    We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...

    Ajax on Rails

    This book teaches intermediate to advanced web developers how to use both Ajax and Rails to quickly build high-performance, scalable applications without being overwhelmed with thousands of lines of ...

    The Rails 5 Way-Leanpub(2017).pdf

    As with previous editions, this book is not a tutorial or basic introduction to Ruby or Rails. The idea is for the full-time Rails developer to give it a once over straight through at first, then use ...

    Rails101_by_rails4.0

    《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...

    the art of rails

    《The Art of Rails》是一本关于Ruby on Rails框架的艺术与实践的书籍。Ruby on Rails是一个使用Ruby语言编写的开源Web应用框架,以模型-视图-控制器(MVC)架构模式为基础。该框架的设计目的是为了使开发者能够快速...

    The art of rails

    《The Art of Rails》是Edward Benson撰写的一本关于Ruby on Rails框架的著作,它深入浅出地揭示了这个强大Web开发平台的艺术与精髓。Rails,作为Ruby语言的一个流行框架,以其“约定优于配置”(Convention Over ...

    Rails之道.pdf(最新版)

    《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容,使读者能够快速地找到对应的API文档、相关的示例代码以及深入的...

    react-rails-form-helpers:用于编写针对Rails的表单的组件

    npm install --save react-rails-form-helpers 通过供应商使用 您可以从获取最新的UMD版本。 关于 该软件包提供了用于编写针对Rails的表单的组件。 该软件包的主要目的是用于通过命名组件传达表单的目的。 如果您对...

    Rails, Angular, Postgres, and Bootstrap(Pragmatic,2016)

    Take a holistic view of full-stack development to create usable, high-performing applications, and learn to use these technologies effectively in a Ruby on Rails environment. Rails is a great tool ...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

Global site tag (gtag.js) - Google Analytics