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

  • Home
  • SEARCH
  • 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 9260385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:52:43+00:00 2026-06-18T12:52:43+00:00

Scenario:A post has many comments. In the index page of posts, when user clicks

  • 0

Scenario:A post has many comments. In the index page of posts, when user clicks the show link( link_to post), its comments will show below the post.

Here I use the append() to add the comments:
$('#edit_post_<%= @post.id %>').append('<%= j render "comments/comments" %>')So when user click the show link, comments would be load and show up.

But how can I hide these comments again (i.e to make comments toggeable) ?

posts/index

<h1>Posts</h1>
<% @posts.each do |post| %>
    <%= form_for post, remote: true do |f| %>
        <%=  post.content %>
        <div id="post_<%= post.id %>">
          <%= link_to "show", post, remote: true  %>
        </div>
    <% end %>
<% end %>

posts/show.js.erb

$('#edit_post_<%= @post.id %>').append('<%= j render "comments/comments" %>')

comments/_comments.html.erb

<% @comments.each do |comment| %>
    <%= comment.content %>
<% end %>
  • 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-06-18T12:52:44+00:00Added an answer on June 18, 2026 at 12:52 pm

    I suppose you dont have the event bindings for new comments.

    you should use something like

    $('.container-of-your-comments').on('click', '.the-toggle-button', function (e) {
      // toggle
    });
    

    so toggling works for all (also future) elements.

    EDIT:

    given the changes in the question: if toggle means just hiding the whole comment list,
    you have to do it manually.

    i suggest you have separate show / hide buttons (you can show and hide the buttons upon changing state as well), because if not, it gets more complex.

    so for example, in posts/index:

    <h1>Posts</h1>
    <% @posts.each do |post| %>
        <%= form_for post, remote: true do |f| %>
            <%=  post.content %>
            <div id="post_<%= post.id %>">
              <%= link_to "show", post, remote: true %>
            </div>
    
            <div class="post-comments" id="post_comments_<%= post.id %>">
            </div>
        <% end %>
    <% end %>
    

    in comments/_comments.html.erb

    <% @comments.each do |comment| %>
        <%= comment.content %>
    <% end %>
    <%= link_to "hide", '#', class: "hide-comments"  %>
    

    and in posts/show.js.erb

    $('#post_comments<%= @post.id %>').html('<%= j render "comments/comments" %>')
    

    and in assets/javascripts/posts.js (be sure you include this in your application.js)

    $(document).ready(function() {
        $('.hide-comments').click(function(e) {
          e.preventDefault();
          $(this).parent().hide();
        });
    });
    

    backbone.js is a great tool, if you’ll be doing a lot of things with those comments, like CRUD etc. and you need to stay on one page – use backbone.js. if not, stick to jQuery.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the scenario: I have a Posts Model and each post has a
Scenario: Every page on the website will have a menu bar at the top
In this simplified scenario, I have a model called Post Each post has a
I have a scenario where I'm updating a blog post which has multiple tags
Consider the scenario where: an ASP.NET webform page has a DropDownList and a ListView.
I'm making a forum in Rails with the traditional Topic has many Posts setup.
In the following link within the 4th image from the top: http://duartes.org/gustavo/blog/post/page-cache-the-affair-between-memory-and-files The scenario
The scenario: a User class has several groups of properties: password , address ,
Scenario Having already read a post on this on the same site, which didn't
My scenario is this: A client application executes a HTTP POST against an endpoint

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.