pagination and search in the same ruby method - ruby

I have the following method in my products controller... the search and paginate seem to be working, but not playing along together. Search is still returning all results, and paginate shows the navigation bar, but that's about it.
def index
#ransack
#search = Product.search(params[:q])
#products = #search.result
#paginate
#product = Product.paginate(:page => params[:page], per_page: 10)
end

ok, I solved my own problem.
#products = #search.result.paginate(page: params[:page] , per_page: 10)

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?) %>

Working on tmdb api on rails

I am continuing my journey in learning ruby and rails. I am currently working with the tmdb gem and trying to access and then return the results of my query.
In my MovieController, I have two methods: search_tmbd and lookup_tmdb. Here is what I have:
def search_tmdb
##movie = Tmdb::Movie.find(params[:search])
#movie = Tmdb::Movie.lookup_tmdb(params[:search])
end
def self.lookup_tmdb(title)
title = params[:search]
#movie = Tmdb::Movie.find(title)
The user inputs the query in :search. When I do the query, I get this error:
undefined method `lookup_tmdb' for Tmdb::Movie:Class.
I understand I may need to pass the values in an array, but how do I work this? This one below works for me in method search_tmdb and returns the title of the movie.
#movie = Tmdb::Movie.find(params[:search])
Change definition to this
def lookup_tmdb(title)
title = params[:search]
#movie = Tmdb::Movie.find(title)
end
and thn call from other function like this
#movie = lookup_tmdb(params[:search])

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.

Resources