undefined method `paginate' for #<Array:0x0000000764d1b8> - ruby-on-rails

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.

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.

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

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)

Sunspot Solr fulltext search and will_paginate

The following query is working like a charm:
#styles = Style.search { fulltext params[:q] }
The problem I'm having is with pagination. Here is the same query with pagination:
#styles = Style.search { fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] }
I have 11 Style records.
If I have :page => 1 and :per_page => 10 when I search for the 11th record, I get an empty array returned for #styles.results
If I set :page=>2 and do the same search I get the 11th style record.
[11] pry(#<StylesController>)> params[:page]=2
=> 2
[12] pry(#<StylesController>)> x=Style.search {fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] }
=> <Sunspot::Search:{:fq=>["type:Style"], :q=>"hel", :fl=>"* score", :qf=>"name_textp full_name_textp", :defType=>"dismax", :start=>10, :rows=>10}>
[13] pry(#<StylesController>)> x.results
=> [#<Style id: 15...>]
I thought the point was to paginate the search results, not the actual records in their entirety
What's going on here, and how do I fix it?
EDIT
Ok, let me try explaining this another way. Let's say I have these six records:
1 => 'a'
2 => 'b'
3 => 'c'
4 => 'd'
5 => 'e'
6 => 'f'
Let's say I try to search for 'f'
Letter.search { fulltext 'f'; paginate :page => 1, :per_page => 5 }
My result will be an empty array []
Now let's say I try
Letter.search { fulltext 'f'; paginate :page => 1, :per_page => 6 }
Now my result is [6 => 'f']
my thoughts:
get the results from solr then search your model by ids that solr returned and paginate them, something like this:
#search = Sunspot.search(Snippet) do
fulltext params[:search]
end
#styles = Style.where(id: #search.results.map(&:id)).page(params[:page]).per(5)
what I understood from docs: I didn't tried it
By default, Sunspot requests the first 30 results from Solr. That means you can have 100 of records that might match the searching criteria but you'll see only the first 30, to see the others you have to add the paginate to your solr search, like:
search = Style.search do
fulltext "my cool style"
paginate :page => 2
end
in this case, you should be able to access 2 page. To update the number of sunspot requests you need to write:
search = Style.search do
fulltext "my cool style"
paginate :page => 1, :per_page => 50
end
it should give you 50 results in one page, or paginate :page => 2, :per_page => 50 and results should be divided in 2 pages with max 50 results each.
Try supply a fallback to per_page like so:
#search = Style.search do
fulltext params[:q]
paginate(page: params[:page], per_page: (params[:per] || 15))
end
I also faced the same problem. So I removed :per_page tag in my search
And changed my query as
#styles = Style.search { fulltext params[:q]; paginate :page => params[:page]}
To set the per_page_limit, I used the following code in my initializers
Sunspot.config.pagination.default_per_page = 20
You can use the "hits" method.
In the controller
#search = User.search do
with :name, 'joe'
end
In the view
<% #search.results.each do |result| %>
<%= result.name %>
<% end %>
<%= will_paginate #search.hits unless #search.nil? %>

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!

Can't sort table in rails

I'm having trouble sorting a single-column table in Rails. Each row represents a single object (an article) and contains all of its attributes (name, content, created_at, user, etc.). The search function works fine (Article.where) but I can't seem to sort the table by any attributes, i.e. Article.order('attribute'). The default, which I can't change, is created_at desc. Am I overlooking something?
Here is my controller:
def index
#title="Home"
if params[:search]
#search=params[:search]
#articles=Article.where('name LIKE ? OR category LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%").paginate(:per_page => 15, :page => params[:page])
else
#articles=Article.order('name').paginate(:per_page => 15, :page => params[:page])
end
end
And view:
<table>
<%= render #articles%>
</table>
<%= will_paginate #articles, :previous_label => "Prev", :next_label => "Next" %>
Use reorder to override any default ordering.
Article.reorder('name').paginate(:per_page => 15, :page => params[:page])
I recommend my gem simple-search for these problems. It may be too simple, but worth a shot.

Resources