is it possible to have format.html and format.json output the same results?

My index action currently looks like this

def index
    @pictures = Picture.search(params[:page])
    respond_to do |format|
      format.html
      format.json { render :text => @pictures.to_json }
    end
end

format.json is used to display the location of pictures on a map. The
javascript code is simple and looks like this:

function listMarkers() {
  var request = GXmlHttp.create();
  request.open('GET', 'pictures.json', true);
  request.onreadystatechange = function() ...

The problem is that the json output always assumes it's on the first
page since it has no idea what params[:page] is. So while the view is
on page 2 the map still shows markers for page 1. Is there a way to
have format.html and format.json output the same @pictures so that the
markers on the map correspond to what's in the view?

vince wrote:

function listMarkers() {
  var request = GXmlHttp.create();
  request.open('GET', 'pictures.json', true);
  request.onreadystatechange = function() ...

The problem is that the json output always assumes it's on the first
page since it has no idea what params[:page] is. So while the view is
on page 2 the map still shows markers for page 1. Is there a way to
have format.html and format.json output the same @pictures so that the
markers on the map correspond to what's in the view?

Your listMarkers function should be passing the current page through.

Fred

Fred - can you explain what you mean by passing the current page
through?

Fred - can you explain what you mean by passing the current page
through?

Well your listMarkers function is requesting pictures.json. You could
rewrite it as
function listMarkers(page){
var request = GXmlHttp.create();
    request.open('GET', 'pictures.json?page='+page, true);
    request.onreadystatechange = function()
}
And then call it with the appropriate parameter
Fred

hmm.. but listMarkers is inside application.js that looks like this:

window.onload = init;
function init() {
  ....
  listMarkers();
}

i don't know how to access the page param from application.js.

I'm no js expert, but off the top of my head you could either fiddle
with window.location, have a bit of js at the top of your rhtml (or
html.erb is we're being rails 2.0) that sets a variable to the current
page, or set window.onload from somewhere in your rhtml.

Fred

well, i don't know how good or scalable my solution is, but i got it
to work by saving the current page in the session (cookie). That way
when the controller gets a json request it can return the results for
the current page number. i feel like this must be a pretty standard
problem with a standard solution being that there are so many sites
out there that display a map alongside search results - any other
suggestions are greatly appreciated.

well, i don't know how good or scalable my solution is, but i got it
to work by saving the current page in the session (cookie). That way
when the controller gets a json request it can return the results for
the current page number. i feel like this must be a pretty standard
problem with a standard solution being that there are so many sites
out there that display a map alongside search results - any other
suggestions are greatly appreciated.

Well it won't work if the same user is trying has 2 browser windows
open pointed at your website :slight_smile:

Fred

dang, you're right - sessions is not going to work. any other ideas?
i don't think i can use window.location in my javascript because the
search function uses rjs/ajax to update @pictures using
"page.replace_html 'results', :partial => 'results' + format.js so
using window.location would still not be the same as what's on the
screen. how does everyone else do this?? :slight_smile: i would think showing a
map next to search results on a website is a problem that's been
solved long ago.

def index
    @pictures = Picture.search(params[:page], params[:tags])
    respond_to do |format|
      format.html
      format.js
      format.json { render :text => @pictures.to_json }
    end
end

dang, you're right - sessions is not going to work. any other ideas?
i don't think i can use window.location in my javascript because the
search function uses rjs/ajax to update @pictures using
"page.replace_html 'results', :partial => 'results' + format.js so
using window.location would still not be the same as what's on the

You could have a javascript variable on the page. the ajax you use to
update the page can also update that variable.

Fred