How to use will_paginate gem with Sinatra and Sequel - ruby

I am trying to use the will_paginate gem to paginate my blog posts, i am also using Sinatra and Sequel for the blog. I receive an error stating
"undefined method `paginate'"
And I have tried everything to make it work, but it's always the same error.
page = params.fetch "page", 1
per_page = params.fetch "per_page", 3
#posts = Post.order(:id).paginate(page.to_i,per_page.to_i)
or
#posts = Post.paginate(:page => params[:page])
Both produce the same error no matter which query I provide.
Is there any way to make this work, or is there any other way so I could paginate my posts using Sequel and Sinatra?

Not specifically Sequel-related, but you can paginate any collection (even those will_paginate doesn't integrate with by default) using this:
require 'will_paginate/collection'
paged_collection =
WillPaginate::Collection.create(page, per_page, total_count) do |pager|
pager.replace(collection)
end
EDIT:
Try this:
require 'will_paginate'
require 'will_paginate/sequel'
require 'sequel/extensions/pagination'
page = params.fetch "page", 1
per_page = params.fetch "per_page", 3
#posts = Post.order(:id).extension(:pagination).paginate(page.to_i,per_page.to_i)

Related

How to display results from database in Sinatra using Sequel?

I have an SQLite3 database called sk.db with a table called Sked that displays a schedule of sports matches with a column date. I am simply trying to display today's matches. It appears as though the connection to the database is not working, though I do not get any errors.
I have tried looking through the Sequel documentation to no avail. How can I display results from an existing database in Sinatra?
.rb
require 'date'
require 'sequel'
require 'sinatra'
DB = Sequel.connect("sqlite://sk.db")
class Sked < Sequel::Model
end
schedule = DB.from(:sked)
get '/' do
todaymatches = schedule.where(:date => Date.today)
erb :games
end
.erb
<h1>Games</h1>
<p><%= #todaymatches %></p>
.where doesn't actually retrieve data, but instead returns a dataset. Add an .all to actually retrieve the data
todaymatches = schedule.where(:date => Date.today).all

Paginate in query

I have inserted a join query in Orders controller like this:
def index
#orders = Order.joins(:user)
.select("users.id AS user_id,users.first_name,users.last_name,users.email,orders.amount,orders.description,orders.created_at")
#count = 0
render :layout => 'orders_layout'
end
How do I paginate the above query?
will_paginate and kaminari are good options. Kaminari also supports mongoid.
Will paginate railscasts and Kaminari railscasts is a good starting point.

How to see paging results with Sequel and Sinatra

I am using will_paginate and attempting to page a dataset from Sequel. The requires are:
require 'will_paginate'
require 'will_paginate/sequel'
require 'will_paginate/collection'
require 'will_paginate/version'
require 'sequel/extensions/pagination'
The Ruby code is:
get '/candidate' do
#items = DB[:candidates].order(:id).extension(:pagination).paginate(1, 10)
erb :candidate
end
In the view: <%= will_paginate #items %>
The dataset renders correctly with 10 records and when I click "2" or "Next" the address in the browser changes to http://localhost:4567/candidate?page=2 but the records remain the same. Effectively, the results are not paged and I cannot get past page 1.
The numbers for the page and the number of records are hardcoded in your example using paginate(1, 10), so it will always bring back page 1 with 10 records. You need to pass on the page=2 parameter from the query-string. This is done via the params helper:
get '/candidate' do
#items = DB[:candidates].order(:id).paginate(:page => params["page"].to_i, :per_page => 10)
erb :candidate
end
If you wanted, you could also pass on the per_page in the query-string by adding this code:
get '/candidate' do
#items = DB[:candidates].order(:id).paginate(:page => params["page"].to_i, :per_page => params["per_page"].to_i)
erb :candidate
end
I'd add a default for both in case they're not given. I understand you can do this via the library, e.g. WillPaginate.per_page = 10, but you could also do this in the route, via:
get '/candidate' do
page = params.fetch "page", 1
per_page = params.fetch "per_page", 10
#items = DB[:candidates].order(:id).paginate(:page => page.to_i, :per_page => per_page.to_i)
erb :candidate
end
I didn't notice before you were also using the Sinatra helper provided by will_paginate.
I'd either call paginate on the dataset or get a dataset, unpaginated, and pass it to the helper. So either this:
get '/candidate' do
page = params.fetch "page", 1
per_page = params.fetch "per_page", 10
#items = DB[:candidates].order(:id).paginate(:page => page.to_i, :per_page => per_page.to_i)
erb :candidate
end
# in the view
<%= #items %>
or this:
get '/candidate' do
#items = DB[:candidates].order(:id)
erb :candidate
end
# in the view
<%= will_paginate #items, params %>
So, from what I can see, the Sequel paginate method is not overwritten or wrapped/overloaded, so its method signature is the same as it would be if you were just using Sequel and not will_paginate as well. Which means that this code worked for me:
require 'will_paginate'
require 'will_paginate/sequel'
get '/candidate' do
page = params.fetch "page", 1
per_page = params.fetch "per_page", 10
#items = Repo.db[:candidates].order(:id).paginate(page.to_i, per_page.to_i)
haml :candidate
end
In the Haml view:
- #items.each do |i|
= i[:title]
Since the method signature is the same I'm not sure what advantage you gain from using will_paginate over Sequel's paginate at all. I couldn't find a way to get the Sinatra helper to work.

undefined method `current_page' for #<Array:0x007fd5ef6dd158> kaminari

Rails 3.2.8 im using kaminari to do pagination, but i keep getting error:
undefined method `current_page' for #
in posts_controller.rb
def index
#posts = Post.order(:created_at).page(params[:page])
end
in views/posts/index.html.erb
<%= paginate #posts %>
what could be the problem?
try to change the code to
#posts = Post.order(:created_at)
Kaminari.paginate_array(#posts).page(params[:page]).per(10)
Or
#posts = Post.order(:created_at).page(params[:page]).per(10)
Kaminari.paginate_array(#posts).page(params[:page]).per(10)
And you can write any number in place of 10 => .per(10)
Let the view code remain same.
will_paginate default comes with pagination for ActiveRecord::Relation but for an Array we need to use this particular method.

Candy for MongoDB

I'm currently experimenting with Sinatra and MongoDB (through the Candy gem). I love the Candy coding style, but I'm having a few issues when I try to retrieve all of the Post objects. Here's my code:
require 'rubygems'
require 'sinatra'
require 'candy'
require 'haml'
Candy.db = "Miroir"
class Post
include Candy::Piece
end
class Posts
include Candy::Collection
collects :post
end
get '/' do
#posts = Posts.all
haml :index
end
When it renders index.haml, all I get is Post (4d0ac53d9b6d4202a3000001){}, and I can't retrieve any of the data. The haml is:
!!! 5
%html
%body
%strong Posts
%ul
- #posts.each do |post|
%li= post
How can I iterate the titles of the posts in the ul? Appreciate the help.
There is a bug in Candy that requires you refresh the object before it'll populate the data when you do a find using a collection.
Do it like so:
- #posts.each do |post|
post.refresh
%li= post

Resources