How do pagination on hash that have value in form of array using Rails 4 - ruby-on-rails

I have hash that have values in form of array. i am using will paginate for pagination. But recieve error
"undefined method paginate for Hash".
below is my code
h = {"127.1.0"=>[31.553965003401203, 74.35798642106394], "127.2.0"=>[54.99947245294339, 74.35810745776595], "127.0.0"=>[51.07178714109552, 71.86443937665145]}
h.paginate(:page => params[:page], :per_page => 30) #giving error
Any help to done this...

It looks like you can use the will_paginate/array module which would at least get you paginating on an array. Try something like this.
require 'will_paginate/array'
h = {"127.1.0"=>[31.553965003401203, 74.35798642106394], "127.2.0"=>[54.99947245294339, 74.35810745776595], "127.0.0"=>[51.07178714109552, 71.86443937665145] }
h.keys.paginate(page: params[:page], per_page: 30)
(similar to Ruby on Rails will_paginate an array)

Related

Ruby on Rails: Show default result before using dropdown filter with will_paginate

I can display the results by 10, 15, or 20 by using a drop-down. The issue is when the app loads it displays all results. I would like to display only 10 results by default before using the drop-down filter to display more results.
Can someone help please?
Thank you!
This is my VIEW:
<%= select_tag :per_page, options_for_select([10,15,20], #per_page), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
This is my CONTROLLER:
#per_page = params[:per_page] || Post.per_page
#posts= Post.all.paginate(:per_page => #per_page, :page => params[:page])
Try this code:
#per_page = params[:per_page] || Post.per_page
#posts= Post.paginate(per_page: #per_page, page: params[:page])
Note that I call paginate without on Post class itself and not on result of Post.all.
I also removed the last || 10 part. I think you don't need it there as you already set this value on Post model.

rails scope multiple params

I have a User model. My goal is to have a scope that returns users found by selected field .
Like
scope :starts_with, ->(filter, starts_with) { where("? like ?", "#{filter}", "#{starts_with}%")}
This is in my view:
<%= select_tag(:filter, options_for_select([["Name", "name"],
["E-mail", "email"]],
"name"), {:multiple => true}) %>
<%= text_field_tag :starts_with, params[:starts_with] %>
UPD I'm calling scope like that:
def index
#users = User.starts_with(params[:filter, :starts_with]).paginate(page: params[:page], per_page: 40)
end
At the moment it shows an error ArgumentError: wrong number of arguments(1 for 0)
Thanks for any help!
Replace your code in your action with:
def index
#users = User.starts_with(params[:filter], params[:starts_with]).paginate(page: params[:page], per_page: 40)
end
What's going on
In your original call you do User.starts_with(params[:filter, :starts_with]). Notice the params[:filter, :starts_with]. You are passing two arguments to the [] method of the params hash. This method only takes one argument. Check out the ruby doc for more info:
http://www.ruby-doc.org/core-2.1.2/Hash.html#method-i-5B-5D
params is a hash like any other hash you might want to use in your app or any ruby code. So, instead of doing params[:filter, :starts_with] that is not authorized, you need to ask for the value of the :filter key and :starts_with key separately and pass them as arguments to your scope like this:
User.starts_with(params[:filter], params[:starts_with])

undefined method `paginate' for #<Array:0x0000000764d1b8>

This my code in the index controller.
order_customs = Order.select{|order| order.performer.white_label_id==1}
#orders_customs_display=order_customs.paginate(:page => params[:page], :per_page => 5)
#orders_customs_refunded=order_customs.select{|order| order.refunded==true}.paginate(:page => params[:page], :per_page => 5)
order_white_label=Order.select{|order| order.performer.white_label_id!=1}
#orders_white_label_display=order_white_label.paginate(:page => params[:page], :per_page => 5)
#orders_white_label_refunded=order_white_label.select{|order| order.refunded==true}.paginate(:page => params[:page], :per_page => 5)
I am using will_paginate gem for pagination. I was using it before without any error but when I changed the code from Order.all.paginate() to Order.select{}.paginate() error is coming.
The error I am getting is
undefined method `paginate'
I need to paginate those values for showing them in a table. If I can't paginate them, is there a workaround?
You need to include the will_paginate method for the data type Array.
To do so, include the line
require 'will_paginate/array'
at the top of your controller, or in the ApplicationController if you need pagination in all of your controllers.
Removing the .all worked for me.

Getting total result count from Rails query using will_paginate

I get a list of objects from my Rails app, and use will_paginate to page as usual, and then I have a little method used to save details of the search to the database:
per_page=10
session[:search_params] = params[:search_people]
#documents = Person.search_people(params[:search_people], params[:page], per_page)
Search.create(:user_id => (!current_user ? 0 : current_user.id),
:search_type => "Person",
:firstname => params[:search_people][:first_name],
:lastname => params[:search_people][:last_name],
:results => #documents.count )
The problem is, the number of search results (#douments.count) is always <= per_page used for will_paginate.
I understand why this is, but is there a way around it without running the query twice, once with will_paginate and once without?
Try <%=#documents.total_entries%>

Paginating a solr response using kaminari?

Does kaminari support pagination of a solr response ? If yes, how to convert the response to a kaminari-compatible format ?
Set up Kaminari for your project as described here - http://railscasts.com/episodes/254-pagination-with-kaminari
In your controller, if you have the following to perform the Solr search -
#vehicles = Vehicles.search do
with (:year => 2012)
paginate :page => params[:page], :per_page => 20
end
You need to add the following in your view to get the pagination links -
<%= paginate #vehicles.hits %>
Hope that helps!

Resources