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:

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?

On Dec 15, 3:47 pm, Frederick C. [email protected]

On Dec 15, 9:26 pm, vince [email protected] wrote:

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

On Dec 16, 1:10 am, vince [email protected] wrote:

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.

On Dec 16, 6:22 am, Frederick C. [email protected]

On 17 Dec 2007, at 03:40, vince wrote:

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

On Dec 16, 11:05 pm, Frederick C. [email protected]

On 17 Dec 2007, at 19:25, vince wrote:

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

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.

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.

On Dec 15, 4:33 pm, Frederick C. [email protected]