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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:30:23+00:00 2026-06-15T16:30:23+00:00

I need to render json with two objects How to do it well? The

  • 0

I need to render json with two objects
How to do it well?

The initial version was:

/controller/comments_controller.rb

    def create
          ....
          respond_to do |format|
            format.html { redirect_to @comment.commentable, flash[:notice] => t('comment.actions.added') }
            format.json { render :json => @comment }
          end
    end

javascripts/comment.js:

submitComment = function(form) {
  $.ajax("/comments/?format=json", {
    type: "post",
    data: form.serializeArray(),
    success: function(comment) {
      $.get("/comments/" + comment.id, function(commentHtml) {
        newComment = $(commentHtml).hide();
        commentsList = $('#' + comment.commentable_type + comment.commentable_id + 'Comments');
        commentsList.append(newComment);
        newComment.show('slow');
      });
      $(form.selector + " textarea").val("");
    },
    error: function() {
      showMessage({
        title: "Error",
        message: "Error occured. Please try resubmit the data."
      });
    }
  });
}

I want to add a dynamic update number of comments,, and I think to do this as:

def create
      ....
      respond_to do |format|
        format.html { redirect_to @comment.commentable, flash[:notice] => t('comment.actions.added') }
        format.json { render :json => {comment: @comment, comments_count: @comment.commentable.comments.count }
      end
end

But I do not understand how to add comments_count to the script javascripts/comment.js.
All my attempts to insert comments_count, like:

$('#comments_count').html(comments_count);

I get an error or the answer “true”

please help me! and thanks in advance!

==== update =====

eicto, thank you, current function is:

submitComment = function(form) {
  $.ajax("/comments/?format=json", {
    type: "post",
    dataType: 'json',
    data: form.serializeArray(),
    success: function(comment) {
      $("h2#comments_count").text(comment.comments_count);
      $.get("/comments/" + comment.comment.id, function(commentHtml) {
        newComment = $(commentHtml).hide();
        commentsList = $('#' + comment.comment.commentable_type + comment.comment.commentable_id + 'Comments');
        commentsList.append(newComment);
        newComment.show('slow');
      });
      $(form.selector + " textarea").val("");

    },
    error: function() {
      showMessage({
        title: "Error",
        message: "Error occured. Please try resubmit the data."
      });
    }
  });
}
  • 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-15T16:30:25+00:00Added an answer on June 15, 2026 at 4:30 pm

    As I understand, you return object like:

    {comment: [], comments_count: 100 }; or it may be {comment: {}, comments_count: 100 };

    anyway here only two root properties of returned object…

    so you should parse it as json and place to element in callback:

    submitComment = function(form) {
      $.ajax("/comments/?format=json", {
        type: "post",
        dataType: 'json', // <- HERE
        data: form.serializeArray(),
        success: function(comment) {
          $('#comments_count').text(comment.comments_count); // <- AND HERE
          $.get("/comments/" + comment.id, function(commentHtml) {
            newComment = $(commentHtml).hide();
            commentsList = $('#' + comment.commentable_type + comment.commentable_id + 'Comments');
            commentsList.append(newComment);
            newComment.show('slow');
          });
          $(form.selector + " textarea").val("");
        },
        error: function() {
          showMessage({
            title: "Error",
            message: "Error occured. Please try resubmit the data."
          });
        }
      });
    }
    

    here are other strange things, like why you interpret comments array as single comment and trying to get property id of it… but it is not related to question.

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

Sidebar

Related Questions

I need to render a partial view to a string within a controller action.
Let's just say that I need to render a certain amount of HTML over
How to format json in rails controller? Via link_to i'm sending and get in
So i need this format json and i have this so far { query:'Li',
I need to render JSON data in my JSPs for some AJAX requests. I'd
I have a rails 3 controller which responds to both HTML and JSON client
I need to render a 1027 * 768 bitmap to the client window (same
I'm using Backbone.Marionette and need to render a view of employees and their respective
I'm writing a game in python with pygame and need to render text onto
Is it possible to add sprite feature in XTK ? I need to render

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.