reverse page numbers with will_paginate - ruby-on-rails

Is there any simple way to reverse page numeration with will_paginate?
I want for the top page (last time-wise) to be #1, and the last (earliest) to be #N.
The reason for that is page contents shouldn't change with time, which is good for SEO.

Order your query by an ascending date instead of a descending date
def index
#posts = Post.paginate :page => params[:page], :order => "created_at ASC"
end

I'm not sure I understand. If the oldest content is #N, then when there's new content, the oldest content will be pushed to #N+1 and the page contents would change. That sounds like it's the opposite of what you're looking for?
You can pass :order => "column ASC" or :order => "column DESC" to paginate to determine what ends up on page #1.

Related

Rails: Next/Previous button for images in database

I have images where you can click next and previous, but I'm having trouble because when I'm viewing the last image in my database (that is associated with user_id), it show's routing error with :id => nil.
This is happening because there's no more data after the image for that user. So how can I set it will rotate their images even if they get to their last image in database (if next), and vice versa, if previous.
This is in my model
def self.s_prev(img)
first(:conditions => ["created_at < ?", img.created_at], :order => "created_at desc")
end
def self.s_next(img)
first(:conditions => ["created_at > ?", img.created_at], :order => "created_at asc")
end
These is my links
<%= link_to "Previous", user_image_path(#image.user_id, #user.images.s_prev(#image)) if user_image_path(#image.user_id, #user.styles.s_prev(#image)) %>
<%= link_to "Next", user_image_path(#image.user_id, #user.images.s_next(#image)) if user_image_path(#image.user_id, #user.images.s_next(#image)) %>
Apparently the if statements don't help me, for some reason
Lets say there's these database id for images table:
id user_id
1 14
2 14
3 14
4 15
So say if I'm on this page: localhost:3000/users/14/images/2
The page will show, and the links will show, but when I click on next, I'll get an error because there is no user_id => 15 and id => 4, where the next button is trying to get localhost:3000/users/14/images/4 for the page in ...users/14/images/3
So how do I avoid this issue and only display next/previous links associated with user?
def self.s_prev(img)
ordered = scoped.order("created_at desc")
ordered.first(:conditions => ["created_at < ?", img.created_at]) || ordered.first
end
def self.s_next(img)
ordered = scoped.order("created_at asc")
ordered.first(:conditions => ["created_at > ?", img.created_at]) || ordered.first
end

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

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.

Ruby on Rails: Default will_paginate to last page of results

Is there an easy way, to default will_paginate to last page? I would like to show user latest addition and allow to browse previuos pages of results...
Just order your query so that it's in reverse chronological order.
Post.paginate(:page => (params[:page] || 1), :per_page => 20 :order => "created_at desc")
The proper way to do it is to reverse the sorting order, i.e. add
:order => 'created_at DESC'
to your paginate call. User would expect the "latest addition" on the beginning, and older ones on the following pages.
This solution was the most resonable idea I found:
http://groups.google.com/group/will_paginate/browse_thread/thread/63b8d295f25085c2

pulling 5 field values from a related model

I have reviewrequests that has many sitereviews. I can get at the count of the number of site reviews a given reviewrequest has in a view with: <%=h request.sitereviews.count.to_s %>
With each sitereview there's a text field - suggestions. Is there a way to get at say the last 5 sitereview.suggestions values as a single value? Something like: <%=h request.sitereviews.suggestions.last.5.to_s %>
You could add this to request.rb
def latest_suggestions
suggestions = []
sitereviews.find(:all, :order => 'created_at desc', :limit => 5).each do |sr|
suggestions << sr.suggestions
end
suggestions.join(',')
end
I'm guessing you mean that each site review has suggestions and you want to get the suggestions of the last 5 site reviews.
This is all pretty weird honestly, not sure why you would want this.

Resources