rails 3,Kaminari pagination for an simple Array - ruby-on-rails

For paginating a common array I got this solution,
#arr_name =
Kaminari.paginate_array(#arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
PER_PAGE_RECORDS is a variable with value as per needed for pagination.
Any better Ideas??
Also to have an ajax call for using pagination one can use this,
In your view,
give id to your div tab
div id="paginate"
and inside it
<%= paginate #arr_name, :remote => true %>
And in js response file put,
$('#paginate').html('<%= escape_javascript(paginate(#arr_name, :remote
=> true).to_s) %>');
So your requests will be AJAX.
Thanks.

This is the only available helper method to paginate an array object using Kaminari. Another alternative is, as suggested solution in kaminari wiki page, add the instance methods to the array object.
If you are trying a common solution based on the ActiveModel return type ( .all returns array and .where returns ARL) then following is an workaround.
unless #arr_name.kind_of?(Array)
#arr_name = #arr_name.page(params[:page]).per(PER_PAGE_RECORDS)
else
#arr_name = Kaminari.paginate_array(#arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
end

Related

acts_as_votable gem likes all the posts on page in rails

Lemme explain what I mean, the point is that I am using acts_as_votable gem in my rails app and it works fyn, problem is that I am using ajax and I am using it to like individual posts from my index page . I will explain with the code
This is my #confessions controller instance variable that has all the votes
#confessions = Confession.where(amitian_id: ids).order('created_at DESC')
Now this is my view that shows all the posts
-#confessions.each do |c|
# code
=link_to pluralize(c.get_upvotes.size,"Like"),like_confession_path(c) , method: :get,remote: true , class: 'like'
=link_to pluralize(c.get_downvotes.size,"Dislike"),dislike_confession_path(c) , method: :get,remote: true ,class: 'dislike'
end
Well, up until now I can use 'c' variable to refer to a single post.. but in my ajax script I have something like this
$('.like').html('<%= pluralize(#confessions.get_upvotes.size,"Like") %>');
$('.dislike').html('<%= pluralize(#confessions.get_downvotes.size,"Dislike") %>');
Now obviously, this will like all the post variable #confessions have.. how can i write a script to make it like the post user clicks on
I suppose I have to use this or self keyword but m not sure how.. help plz
Move the confessions block to a separate partial. Then wrap that partial in a div. Then in your ajax script you can just reload the partial.
View:
<div id=confessions>
<%= render 'confessions' %>
</div>
Ajax
$('#confessions').html("<%= escape_javascript(render 'confessions') %>")
Also, are you sure you want to be making GET requests to like/dislike? I think you should be making a POST request here.

rails form_tag leaving sticky params in pagination url generated by the kaminari gem

I have a form_tag declaration:
<%= form_tag(:action => :index) do %>
It was working fine until I upgraded to Rails 4. Now all the params from this form continue to stay in the address bar, and forcing their value on all future invocations of this form.
I have managed to workaround for now by adding the following code to the controller after consuming these params:
arr = %i(prev_bal as_of amtpaid pmt_date apr authenticity_token commit int_day new_curbal utf8)
arr.each { |sym| params[sym] = nil }
I would appreciate a cleaner way to resolve this.
Thanks for taking the time to help.
UPDATE
The pagination links created by kaminari are the only ones with the params in the url. The only param those links should have is ?page=.

Does will pagination work with forms which have method="POST"?

I switched my advanced order form to POST from GET as the URI request became too large for browsers to handle. Everything works fine, with the exception of will pagaination. It keeps adding the page to the url itself http://localhost:3000/orders/advanced_search like http://localhost:3000/orders/advanced_search?page=2 which fails as this is a post call, and not a get call.
Any way that it can just update the params[:page] but not do anything to the link?
I used to just call <%= will_paginate #orders["order_items"] %> which worked great when it was a GET call where
#orders["order_items"] = #orders["order_items"].paginate(:page => params[:page],
:per_page => limit, :total_entries=>#orders["total"])
What I want is simple, add it to params, but do not add it to the link.
The other answer posted is wrong. will_paginate was NOT built to work with post requests.
Your choices to 'make' will_paginate work with post request include:
writing some javascript to:
preventDefault on click of the will_paginate-generated link
$(".pagination li a").click(function(e){
e.preventDefault(); ....
find the target page via the clicked link.
(below code is assuming all will_paginate links have a query string...which they do. But note that this particular line of code won't work if you are passing in more params in the controller via the params option for will_paginate..which you shouldn't be in the first place because we are trying to achieve a post request.)
var tp_id = $(this).attr("href").split('?page=')[1]
generate a hidden input with the correct name attribute depending on the clicked will_paginate link.
$('form').append("< input type='hidden' name='page' value='" + tp_id +"' >")
finally post to the controller action of the search form.
$('form').submit()
.done. I hope this puts you on the right path.
You can add a param to will_paginate
https://github.com/mislav/will_paginate/wiki/API-documentation
<%= will_paginate #orders['order_items'], :params => { :method => :post } %>
If it doesn't work, set te complete controller and action to params.

kaminari ajax pagination not updating the paginate

I'm implementing pagination in rails3 using the kaminari gem.
I've been following this code from github
https://github.com/amatsuda/kaminari_example/commits/ajax
unfortunately, the following code
jQuery('#paginator').html('<%= escape_javascript(paginate(#recipes,:remote=>true).to_s) %>');
does not seem to be working.
my javascript for what to do what a user selects a page is
jQuery('a','nav.pagination').click(function(){
// get the page value from href
var page = jQuery(this).attr('href').replace('/home/index?page=','');
jQuery.getJSON('?page='+page, function(data){
for (var r in data){
results.push(data[r]);
});
showResults(results,(page-1)*10);
jQuery('#paginator').html('<%= escape_javascript(paginate(#recipes,:remote=>true).to_s) %>');
});
the showResults function runs through the JSON and creates a bunch of html elements.
Everything is working great, except that the pagination isn't updating to show the current page when the results are reloaded.
I don't get any errors in firebug, i'm using Rails 3 with ruby 192.
Are you sure you have '#paginator' element for the paginator in you HTML?
I mean, does the following code return the element on Firebug?
jQuery('#paginator')
In order to update result and paginate you need to update both at a time.
Paginate AJAX sends a request as JS so to handle them you should have action.js.erb file inside view folder. Perform two things here.
1) Update result
$("#issues").html("<%= j render(:partial => 'recipes') %>");
2) Update Paginate
$('.pagination').html('<%= escape_javascript(paginate(#recipes, :remote => true).to_s) %>');
Find respective page attribute to update paginate. This will update result and paginate run time.

Rails 3 Pagination for NON-ActiveRecord results

My rails 3 app uses sphinx to perform search on text files, and I return a collection of results on the landing page view: something like
<%= render :partial => "result", :collection => #results %>
What is the Rails way to do pagination for the results? I know there are plugins like will_paginate, but I'm not using ActiveRecord (my app doesn't even use a db).
Is a Javascript solution the best way to go? I want to also maintain REST in the url, ie "/search?query=hello&page=2"
Here is someone who has already done so: How to use will_paginate with non-ActiveRecord collection/array

Resources