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');
});
 });
jay
  • 12,066
  • 16
  • 64
  • 103

1 Answers1

0

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

Community
  • 1
  • 1
Jeff Paquette
  • 7,089
  • 2
  • 31
  • 40
  • ok, so I guess I could save a user id in my session, and then delete it after. However, are there any other better ways? For example, when "show" gets called, you can find a user from params[:id]. Maybe (if I learnt how, of course) i could use that somehow? thanks for the help Jeff – jay Jul 24 '11 at 02:28
  • See my (rather short) edit. Can you post the code that performs the ajax request? – Jeff Paquette Jul 24 '11 at 03:02
  • I currently use a js file to make the request when a div #show_discussions is clicked. – jay Jul 24 '11 at 03:18
  • yes, I think that might work.. except i don't think i can access @user from a .js file (nor write any ruby in), so I would have to write each link manually. – jay Jul 24 '11 at 03:44
  • You could write a js var that holds the user id into the view when you generate the page, and access it from your other script. – Jeff Paquette Jul 24 '11 at 12:35
  • hmm that sounds like a good option, do you have any suggestions for where to read about writing js variables? – jay Jul 24 '11 at 13:43