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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:39:21+00:00 2026-05-23T23:39:21+00:00

I would like to use jQuery to refresh a partial on a page, but

  • 0

I would like to use jQuery to refresh a partial on a page, but I can’t get the partial to work because the collection it requires isn’t available after the jQuery code executes.

I have a show action in the Users controller:

  def show
      @user = User.find(params[:id])
      @title = "#{@user.first_name} #{@user.last_name}"
      @posts = @user.posts.paginate(:page => params[:page], :per_page => 20 )
      @discussions = @user.discussions.paginate(:page => params[:page], :per_page => 2)
    end

This makes the collections @posts and @discussions available on the user show page to pass to partials as follows, in show.html.erb:

<div id="synopsis">
    <%= render :partial => 'users/post', :collection => @posts %>
</div>
<div class="pagination_links">
    <%= will_paginate(@posts) %>
</div>

However, let’s say I want to replace the posts showing on the user show page to the user’s discussions, ie using @discussions available in the show action. To do so, I set up a custom action in the Users controller called show_discussions and made the routes to it work. this is the action:

def show_discussions
  respond_to do |format|
    format.js
  end
end

This references show_discussion.js.erb

$("#synopsis").html("<%= (escape_javascript("#{render(:partial => 'discussion_activity', :collection => @discussions)}")).html_safe %>");

This file successfully replaces #synopsis, but fills it with nothing – as the collection @discussions doesn’t persist through the remote call to this action. How can I fix this such that the users discussions are made available after the remote call?

UPDATE: I make the ajax request using a js file. I specify to get the user action show_discussions when a button is clicked:

 $(function() {
$('#show_discussions').live("click", function() {
    $.get('/users/show_discussions');
});
    $('#show_posts').live("click", function() {
    $.get('/users/show_posts');
});
 });
  • 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-23T23:39:22+00:00Added an answer on May 23, 2026 at 11:39 pm

    Remember your app has no context from one request to the next (well, except for what’s in your session, and your post variables). Whatever you need to render a view, you must provide in the controller action.

    You can pass any needed context, such as the user id, as a parameter on your ajax request.

    For an example, see the first answer to this question

    Since you’re using GET instead of POST, you can just tack the user id onto the url when writing it into the view. Your script should end up looking like this:

    $.get('/users/show_discussions/123);
    

    where 123 is the id of the user.

    To write the user id into the view as a javascript variable, you can do something like:

    <script>
      var jsUserID = <%= @user.id %> ;
    </script>
    

    if you have just one user id on the page. Your link code becomes:

     $.get('/users/show_discussions/' + jsUserID);  
    

    TL;DR: you need to load @discussions in show_discussions

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

Sidebar

Related Questions

I would like to .get a page and then use jquery to find elements
I would like to know how can I use jQuery to select multiple items
I would like to know if I can use the following JQuery function on
I would like to use jQuery in ASP.NET User Control. Can some one tell
I am building a dynamic partial load asp.net page, I would like to use
I would like to append some code to a page using jQuery/jQuery mobile, but
I would like to use jQuery's load function. I can write: $('#somediv').load('somefile.html #aSpecificDivinThatPage'); Now
I would like to use jQuery to get the X/Y coordinates of a click
I would like to use jQuery UI tabs but I need the tabs aligned
I am relatively new to using jQuery and would like to use the load

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.