Similar topic to my last post, but a different brand of methods. In
particular, the url_for and link_to methods have come to bother me at
times. I will mention one instance in particular:
I am designing a high-end administrative interface. This interface
supports paging, sorting and limiting to specific field values. Thus I
have params named page, sort and limit.
The lack of elegance I see comes when I make new links or add new
parameters. To keep the administrator on the same page time and time
again, I have to add “:page => params[:page]” to every link in my
scaffold’s edit.rhtml, list.rhtml and new.rhtml as well as to my helpers
that generate paging, sorting and limiting links. As I add more
parameters, the link_to commands in these files grow more and more
unreadable and the number of places I have to explicitly specify every
parameter in the query string increases.
I attempted to use :overwrite_params, but then all of a sudden link_to
does not seem to recognize my routes. It starts dumping URL-defined
params into the query string.
When I read about link_to, the description said that it intuitively
understood the hierarchy of parameters. If the :action changed, the :id
was nulled. If the :controller changed, :action and :id were nulled. And
The problem is once you get into params in the query string such as
params[:page]. It seems to me that a link_to specifying only :sort =>
sort should not overwrite :page or :limit. Params in the query string
should act as brothers. No one param should be the parent of another;
setting one should not unset the others. They are not in a hierarchy.
They merely exist side-by-side. Changing the way rails nulls query
string parameters would keep those params out of my paging, sorting and
limiting helpers, which I feel should ideally not have to know about
each other. And if there is some reason this should not be the default,
perhaps it should at least be an option. Or perhaps :overwrite_params
needs to be fixed. Or perhaps I am using it incorrectly?
This also wouldn’t address every repeated instance of “:page =>
params[:page]” in my views. Changing :action still needs to null query
string parameters. Or does it? Should query string parameters be
considered children of even :action or :controller? Is :page necessarily
subordinate to :action => ‘list’? To me it seems :page should be
subordinate to the :controller but not the action. Perhaps some method
of explicitly specifying query string parameter hierarchies is in order.