vince
2007-Dec-15 20:32 UTC
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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-15 20:47 UTC
Re: is it possible to have format.html and format.json output the same results?
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vince
2007-Dec-15 21:26 UTC
Re: is it possible to have format.html and format.json output the same results?
Fred - can you explain what you mean by passing the current page through? On Dec 15, 3:47 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-15 21:33 UTC
Re: is it possible to have format.html and format.json output the same results?
On Dec 15, 9:26 pm, vince <stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 15, 3:47 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vince
2007-Dec-16 01:10 UTC
Re: is it possible to have format.html and format.json output the same results?
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 Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On Dec 15, 9:26 pm, vince
<stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 15, 3:47 pm, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > wrote:
>
> > > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-16 11:22 UTC
Re: is it possible to have format.html and format.json output the same results?
On Dec 16, 1:10 am, vince <stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vince
2007-Dec-17 03:40 UTC
Re: is it possible to have format.html and format.json output the same results?
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 16, 1:10 am, vince <stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-17 04:05 UTC
Re: is it possible to have format.html and format.json output the same results?
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 :-) Fred> > > On Dec 16, 6:22 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On Dec 16, 1:10 am, vince <stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
vince
2007-Dec-17 19:25 UTC
Re: is it possible to have format.html and format.json output the same results?
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?? :) 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 Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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 :-)
>
> Fred
>
>
>
> > On Dec 16, 6:22 am, Frederick Cheung
<frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> > wrote:
> >> On Dec 16, 1:10 am, vince
<stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.
>
> >> Freda
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-17 19:38 UTC
Re: is it possible to have format.html and format.json output the same results?
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 theYou 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?? :) 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> 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 :-) >> >> Fred >> >> >> >>> On Dec 16, 6:22 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> wrote: >>>> On Dec 16, 1:10 am, vince <stha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. >> >>>> Freda > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---