Rails Automatically Save a Draft of a Form
Gmail里的Ajax自动保存草稿在Rails里的实现
首先在layout里引入Javascript标签
[coce]
<%= javascript_include_tag :defaults %>
posts_controller.rb
posts/new.rhtml
首先在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
- <% 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 %>
莫愁前路无知己,天下无人不识君。