ddnexus / pagy

🏆 The Best Pagination Ruby Gem 🥇
https://ddnexus.github.io/pagy
MIT License
4.62k stars 412 forks source link

pagy(..., page: #) seems to be ignored #97

Closed ghost closed 6 years ago

ghost commented 6 years ago

I'm setting the page number manually with pagy(Collection.all, page: page) in my view (my site framework doesn't use a params variable), but this generates a NameError: "undefined local variable or method params". I get the same error when I don't specify the page option, so I'm guessing page is being ignored?

ddnexus commented 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.

ghost commented 6 years ago

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?

ddnexus commented 6 years ago

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 .

ddnexus commented 6 years ago

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?

ddnexus commented 6 years ago

You may also want to read the Pagy::Backend doc that could give you more insights about the pagy method.

ghost commented 6 years ago

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.

ddnexus commented 6 years ago

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).