Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6680333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:27:47+00:00 2026-05-26T04:27:47+00:00

I have a Comment model that I want people to be able to save

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:27:48+00:00Added an answer on May 26, 2026 at 4:27 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an Comment model that belongs_to a Post model. I want to
Let's say I have 3 models (Note, Comment, Article) that I want to search
I have comment model that references (foreign key) a post model and there is
I have a model User that has defined in user.rb many relationships like has_many
I have a Comment Model that I am using to store comments for both
Let's say you have a SiteUpdate and a Comment model, and you want to
assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i
I have a website where people can comment and each comment has a category
I have some model objects that I save in a DB serialized with protobuf.
I have a model that I want to be commentable. I am having difficulty

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.