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 6619087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:55:18+00:00 2026-05-25T20:55:18+00:00

I am working on Rails 3.1 Jquery Ajax demo app listed here https://github.com/samnang/ajax_rails31_demo .

  • 0

I am working on Rails 3.1 Jquery Ajax demo app listed here https://github.com/samnang/ajax_rails31_demo. I am trying to add code to delete comments. I have modified comments_controller.rb and added a destroy action. I also added a link to the app/views/_comment.html.erb to include a Destroy link. I am able to get the comment to delete and the comment count is decremented by 1, but the comment partial does not update removing the entry. Only upon page refresh does the page display correctly. Here is my code

app/controllers/comments_controller.rb

class CommentsController < ApplicationController
  respond_to :html, :js

  def index
    @comments = Comment.all
  end

  def create
    @comment = Comment.new(params[:comment])
    @comment.save

    respond_with @comment, :location => comments_url
  end

  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_with @comment, :location => comments_url
  end
end

app/views/comments/index.html.erb

<% title "Comments for Ajax in Rails 3.1" %>

<div id="comments_count"><%= comments_count %></div>

<div id="comments">
   <%= render @comments %>
</div>

<h3>Add your comment:</h3>

<%= form_for Comment.new, :remote => true do |f| %>
<%= f.error_messages %>
   <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
   </p>
   <p>
    <%= f.label :content, "Comment" %><br />
    <%= f.text_area :content, :rows => '12', :cols => 35 %>
   </p>
  <p><%= f.submit %></p>
<% end %>

app/views/comments/_comment.html.erb

<div class="comment">
  <strong><%= comment.name %></strong>
  <em><%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
  <%= simple_format comment.content %>
  <%= link_to "Destroy", comment,:remote => true,  :confirm => "You Sure", :method => :delete %>
</div>

app/vews/comments/create.js.coffee

$('<%= escape_javascript(render(:partial => @comment))%>')
  .appendTo('#comments')
  .hide()
  .fadeIn()

$('#new_comment')[0].reset()

$('#comments_count').html '<%= comments_count %>'

app/views/comments/destroy.js.coffee

$('#new_comment')[0].reset()

$('#comments_count').html '<%= comments_count %>'

The destroy jquery portion is where I believe I am messing up. How do I reload the partial to remove the deleted comment?

  • 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-25T20:55:18+00:00Added an answer on May 25, 2026 at 8:55 pm

    I have updated my example on Github to support destroy action. In your example you didn’t remove dom element in your destroy.js.coffee. Check out full code on Github again or look at below snippets:

    app/views/comments/_comment.html.erb

    <div id=<%= dom_id(comment) %> class="comment">
      <strong><%= comment.name %></strong>
      <em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
      <%= link_to "Remove", comment, :method => :delete, :remote => true %>
      <%= simple_format comment.content %>
    </div>
    

    app/views/comments/destroy.js.coffee

    $('#comments_count').html '<%= comments_count %>'
    
    $('#<%= dom_id(@comment) %>')
      .fadeOut ->
        $(this).remove()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rails 3 app.... I have the following jQuery which is working: $.ajax({ url: '/navigations/sidenav',
I am working on a simple Rails/jQuery HTML templater app which stores a series
So I'm trying to get some basic jQuery code with Rails working, but it
I am trying to use jQuery with Rails and AJAX to accept a post
I have jquery working on my Rails app using the gem 'jquery-rails' . I
I'm working on a rails 3 app and trying to deploy it on a
I am working on a rails app, where I am using jQuery and the
I am working on rails project and I am trying to get exceptions to
I'm working on a Rails app and am looking to include some functionality from
I'm working on a rails app that has a whole bunch of before filters

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.