Just wasted two hours trying to save the current page in the session. My first approach was stolen from the scaffolded view: session[:page] = @person_pages.current But - as I discovered later - this saves a Page object, not only the page number. To save the current page I had to use session[:page] = @person_pages.current.number Scaffolding generates links like: <%= link_to ''Previous page'', { :page => @person_pages.current.previous } if @person_pages.current.previous %> and generates a number (the page number) as action parameter. Can someone explain shortly why the (working) code generated by scaffolding in the view evaluates @person_pages.current.previous (a Page object) to a number whereas @person_pages.current (also a Page object) called in the action returns the Page object ? Thanks, Thomas --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It probably does a to_s on it which returns a ''number''. Vish On 10/1/06, Thomas <twenrich-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Just wasted two hours trying to save the current page in the session. > > My first approach was stolen from the scaffolded view: > session[:page] = @person_pages.current > But - as I discovered later - this saves a Page object, not only the > page number. To save the current page I had to use > session[:page] = @person_pages.current.number > > Scaffolding generates links like: > <%= link_to ''Previous page'', > { :page => @person_pages.current.previous } if > @person_pages.current.previous %> > and generates a number (the page number) as action parameter. > > Can someone explain shortly why the (working) code generated by > scaffolding in the view evaluates @person_pages.current.previous (a > Page object) to a number whereas @person_pages.current (also a Page > object) called in the action returns the Page object ? > > Thanks, > Thomas > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2006-Oct-01 14:39 UTC
Re: don''t understand Paginator/Page or Ruby construct
On Oct 1, 2006, at 16:29, Thomas wrote:> Can someone explain shortly why the (working) code generated by > scaffolding in the view evaluates @person_pages.current.previous (a > Page object) to a number whereas @person_pages.current (also a Page > object) called in the action returns the Page object ?Views default to calling to_s on objects to get the value to render, hence <%= @person_pages.current %> in a view is really the return value of @person_pages.current.to_s. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/1/06, Jakob Skjerning <jakob-pixy5vpirPnEueBKFXcDjA@public.gmane.org> wrote:> > > > On Oct 1, 2006, at 16:29, Thomas wrote: > > > Can someone explain shortly why the (working) code generated by > > scaffolding in the view evaluates @person_pages.current.previous (a > > Page object) to a number whereas @person_pages.current (also a Page > > object) called in the action returns the Page object ? > > Views default to calling to_s on objects to get the value to render, > hence <%= @person_pages.current %> in a view is really the return > value of @person_pages.current.to_s. >Thank you very much. In my opionion, it''s a bug. With my limited experience in Rails and Ruby, I have seen the view as "the thing displaying my instance vars" and the code written in the view as "another method of the controller". Now I discover that something too much magic happens and the code in the views are interpreted differently, so forcing me to remember an unnecessary detail ("remember always - coding in ''Controller'' or ''View'' context ?", "be aware when you copy/paste some expressions"). As the real fix (either remove to_s from Pages or the magic from the view interpreter) most likely will break existing code, maybe it would be nice when at least the scaffolder adds ''to_s'' ? Thomas --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---