Closed ghost closed 6 years ago
The :page
variable does not get ignored: the execution doesn't even arrive to create the Pagy
instance, because the Pagy::Backend
is assuming there is a params
method in your controller.
Please, take a look at the Environment Assumptions for clue about why that happens and how to easily avoid it.
From that page:
However you can force a page number by just passing it to the pagy method. For example:
@pagy, @records = pagy(my_scope, page: 3)
That will explicitly set the:page
variable, overriding the default behavior (which usually pulls the page number from theparams[:page]
).
Doesn't this mean that I don't need a params
variable/method at all, if I am always explicitly setting the page number?
Forcing/overriding the page number happens AFTER the params method is called.
On Thu, Oct 18, 2018, 12:38 PM Polyethylene notifications@github.com wrote:
From that page:
However you can force a page number by just passing it to the pagy method. For example: @pagy, @records = pagy(my_scope, page: 3) That will explicitly set the :page variable, overriding the default behavior (which usually pulls the page number from the params[:page]). Doesn't this mean that I don't need a params variable/method at all, if I am always explicitly setting the page number?
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ddnexus/pagy/issues/97#issuecomment-430881265, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGJcSNL9kbCuw95AlXBZbgIg61bqylNks5umBO8gaJpZM4XsrBS .
In other words: you have first to satisfy the environment assumptions (which require a params
method or an overridden pagy_get_vars
that avoids to call it), and then if you want explicitly override the :page
variable you can pass it to the pagy
method. However that is for single instance overriding or variable addition.
Since you have to override the pagy_get_vars
to avoid the params
call, I would suggest you to set the :page
in that method, which raison d'être is exactly collecting the variables (including the :page
) for the Pagy
creation.
BTW, what framework are you using and where do you get the :page
from?
You may also want to read the Pagy::Backend doc that could give you more insights about the pagy
method.
Thanks for clearing that up. I managed to set the page number by overriding pagy_get_vars
so the page
option is given preference, as this seems to be the only way to get it working with the framework I'm using.
I'm using Middleman and Sequel together. The page number is passed as a local variable directly to a template, and bypasses the file that overrides pagy_get_vars
. That's why it wasn't clear how to set it up.
Glad that you made it work.
However, overriding pagy_get_vars
is not the only way. You can write your own pagy
method and bypass the pagy_get_*
methods altogether.
For sequel you need also to override he :count
assignment from collection.count(:all)
to collection.count
(but you should already know that by now).
I'm setting the page number manually with
pagy(Collection.all, page: page)
in my view (my site framework doesn't use aparams
variable), but this generates a NameError: "undefined local variable or methodparams
". I get the same error when I don't specify thepage
option, so I'm guessingpage
is being ignored?