开发者

How can I change the action a form submits to in a jQuery autosave submission? (In Rails)

开发者 https://www.devze.com 2023-04-12 19:02 出处:网络
I have a Comment model that I want people to be able to save drafts for. It has a boolean attribute \"draft\" that allows the comment to be saved (but not show up yet). I\'m creating an autosave funct

I have a Comment model that I want people to be able to save drafts for. It has a boolean attribute "draft" that allows the comment to be saved (but not show up yet). I'm creating an autosave function for these comments.

The Comment form current runs like this: the variable @comment is initialized by the controller as @comment = Comment.new. Then the form for the comment is:

  <%= form_for @comment, :remote => true do |f| %>
      <%= f.text_area :title, :class => "inputform" %>
      <%= f.text_area :content, :class =>"inputform" %>
      <%= f.submit "Submit", :class => "button" %>
  <% end %>

So, as I said I want this to autosave. To start accomplishing it, I wrote this autosave_comments.js file:

 $(document).ready(function(){
        setInterval(function() {
             $('new_comment .temp').html('<input type="hidden" name="comment[draft]" id="comment_draft" value="true" />');
             $('#comment_form form[data-remote]').submit();
             $('new_comment .temp').html('');
        }, 10000);
  });

This code sets an input for draft to be true, submits the form, and then removes that input for draft. This code works great, as it submits the form and saves a draft to the controller. HOWEVER, every submission saves a new entry (ie every 10s a new comment is saved in the database as a draft), rather than updating the first entry.

A final bit of background: when the form is submitted to the comments controller, it submits to the create action:

 def create
     @comment = params[:comment]
     if @post.save
         if params[:draft]
              flash.now[:notice] = "draft autosaved"
         else
              flash.now[:success] = "comment created"
         end
     else
         #code to output errors
     end
     respond_to do |format|
           format.html 
           format.js开发者_开发百科
     end
  end

This then references a create.js.erb file:

    <% post = user.posts.last %>
    <% if post.draft == false %>
        //code here deals with a true submission of a comment, to append tables etc.
    <% else %>
        //maybe some code here could alter the form on draft submission to make it update the same post next time?
    <% end %>

SO I am wondering, I would like for the first draft submission to work as it does and create an entry in the Comments table. BUT then I want the form to update that comment on subsequent autosaves, and to save the comment as a non-draft final comment when the person submits the comment for posting. Is there someplace in one of these files that I have outline that I could accomplish this?

Thanks!


In create.js.erb:

$('#comment_form').attr('action', '<%= comment_path(@comment) %>');

Just make sure that your comment form has an ID of comment_form, or otherwise change the jQuery object accordingly. Rails should take care of the rest.

edit I forgot that you'll need to also fake the PUT request like Rails does. Since some browsers don't support PUT requests, rails uses POST and then adds a hidden form field:

<input name="_method" type="hidden" value="put" />

So just generate that in your create.js.erb:

$('#comment_form').append('<input name="_method" type="hidden" value="put" />');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号