'Why does Pagy return nil in Sinatra?
@pagy is returning nil in my view (#<NoMethodError: undefined method 'pages' for nil:NilClass>).
It works as expected in the console:
> @pagy.pages
=> 25
> pagy_nav(@pagy)
=> "<nav class=\"pagy-nav pagination\"...
I've added the Pagy includes in the Folders Controller:
class FoldersController < ApplicationController
include Pagy::Backend
get '/folder/:permalink' do
@pagy, @pagination = pagy(Article.all)
end
helpers do
include Pagy::Frontend
end
end
I'm calling it in the Folder's Show View:
<%= pagy_nav(@pagy) if @pagy.pages > 1 %>
Any ideas why @pagy is returning nil inside the view?
Any advice on using Pagy in a Sinatra app?
Solution 1:[1]
@pagy needs to be called before your erb call.
Like this:
@pagy, @articles = pagy(@articles.order(position: :desc), items: 50)
erb :'folders/show_folder'
Not like this:
erb :'folders/show_folder'
@pagy, @articles = pagy(@articles.order(position: :desc), items: 50)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brad West |
