don't understand Paginator/Page or Ruby construct

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 ?


It probably does a to_s on it which returns a ‘number’.


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’ ?