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

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.

Related

Rails dummy object added to Active Record Array

I use rails 4.2 and I found a weird issue:
#tweet = current_user.tweets.new
#tweets = current_user.tweets
When I loop over in Views like:
<%= render #tweets %>
I get an Extra Record with null id.
Example:
You build this empty Tweet yourself in your controller:
#tweet = current_user.tweets.new
#tweets = current_user.tweets
There are several ways to avoid this problem. You could build the new Tweet without adding it to the #tweets array:
#tweet = Tweet.new(user: current_user)
#tweets = current_user.tweets
Or you could change your your to exclude tweets that haven't been saved to the database yet:
<%= render #tweets.select(&:persistent?) %>

How to display contents from a sequel database connection in the view file in Ruby?

I am trying to retrieve data from a PostgreSQL database with Sequel in Sinatra.
DB = Sequel.connect('postgres://connection_data')
items = DB[:items]
Then I try to get an entry with a specific ID:
get '/:id' do
#item = items.filter(:id => params[:id])
erb :edit
end
In my edit view I would like to display the content of the #item variable. The problem is that I don´t know how to get for example the ID.
<% if #item %>
Do something
<% else %>
<p>Item not found.</p>
<% end %>
I tried using #item.id and #item[:id] but both don´t work. I get an error undefined method 'id' for #<Sequel::Postgres::Dataset:0x007fac118b7120>. What would be the right way to retrieve the values from the #item variable?
#item = items.filter(:id => params[:id]) returns a dataset. If you want a single item, you should do: #item = items.first(:id => params[:id].to_i)
Also #item.id is probably not want you want. Given that items = DB[:items], you are using a plain dataset and then #item = items.first(:id => params[:id].to_i) is going to give you a hash. You need to do #item[:id] to get the item's id.
You may want to look at using models instead:
# model file
class Item < Sequel::Model; end
# sinatra code
#item = Item[params[:id].to_i]
# template
#item.id
Actually #item.id is the right way. The only problem I can see in your code is
#item = items.filter(:id == params[:id])
which should be
#item = items.filter(:id => params[:id].to_i)
EDIT:
Try this:
#item = items.where(:id => params[:id].to_i)
#item.select(:id) #to embed
params[:id] is giving a string, so convert it to an integer.

How to use will_paginate gem with Sinatra and Sequel

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)

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.

Resources