pagination - Display object rank count in views with rails -


i have page displaying list of movie. movies voted on users , in order of vote count. i'm using pagination, split every 15 movies. need put basic rank field under each movie.

movie name rank #1

movie name rank #2

movie name rank #3

i can't think of best way this. tried use each index , display index, pagination breaks this. , count starts new every time pagination moved next group.

does have ideas of how might accomplish this?

thanks in advance.

sorry not posting code in first place. scrapped each index idea wasn't working way expected. moved making rank method on movie class still producing odd outcome.

** movie.rb

def rank
movie.all.order(vote_count: :desc)
end

** index.html.erb

<%= @movie.rank %>


when call adds rank, if 2 movies have same amount of votes ranks still out of order.

also apologize poor stack overflow etiquette. review guidelines, , better in future.

controller:

def index   @movies = movie.order("vote_count desc").paginate(....) end 

view:

-@movies.each_with_index |movie, i|   =movie.name   %span ranked number #{@page-1 * per_page + i} 

explanation inside controller paginate collection page parameter (or default 1). paginate plugin adding limit sql-query. lets have 100 results , need page 5 (assuming each page 10 entries), need 10 entries, starting @ number 40. in sql looks limit 40, 10.

if have set, go view , render them. rank calculated page-1 * per_page + current_index. current index starting 0 , ends n-1.

lets assume go 40, page 5. means (5-1) * 10 offset, in case 40. adding index, 0,1,2 coming 40, 41, 42, 43....

hope helps


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -