Adding pagination to index.html.erb in Ruby on Rails is a simple question

I'm new to rails, so easy. I created my first blog and would like to set it up so that in <% post.each do | post | %> render posts, I would like it to work so that it only displays 10 posts and then has a link to view the next 10.

I hope this is a simple question. Let me know if you want me to post any code.

+4
source share
7 answers

will_paginate is definitely the way to go, I just thought I would add a link to the railscasts will_paginate video showing you how to use it, as it can sometimes be an easier way to learn the basics than reading the documentation. In addition, you will learn a brief history of how pagination was performed when it was part of Rails (it was slower and cumbersome). The old classic pagination was moved to its own plugin, but it is only recommended if you have already used it when upgrading Rails to the version where they pulled it.

Railscasts has tons of great videos that you might find useful. For example, as soon as you move to a more advanced level, you can try ajax pagination

+7

will_paginate Gem.

ActiveRecord:

@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'

:

<%= will_paginate @posts %>
+4

"will_paginate", GEM, unistalling... ! RoR, , , :

:

  def index
#how many register per page i want to show
    @b = 30 
#params via GET from URL
    a = params[:page].to_i
#first register per page
    a1 = params[:page].to_i * @b
#the query can be easy...
    @callcenters = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order => "created_at DESC",:limit => "#{a1},#{@b}", :select => "radcheck.username as clave,caller_id, created_at, value")
#I need to know how many pages somehow
     @registrostotales = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select => "radcheck.username as clave,caller_id, created_at, value").count

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @callcenters }
    end
  end

:

...
Total de registros: <%= numero = @registrostotales %><br/><br/>
<!-- total pages -->
<% pages = @registrostotales / @b %>
<!-- the last page -->
<% if pages % @b > 0 %><% pages = pages + 1 %><% end %>

Paginas:
<% (0..(pages-1)).each do |i| %>
<!-- href or link_to, http://xxxxxxxx/yyyy?page=i -->
  <%= link_to i+1, :action => "index", :controller => "callcenters", :page => i %>
<% end %>


<!-- the view.. -->
<table>
  <tr>
    <th>#</th>
    <th>Tel&eacute;fono (ID)</th>
    <th>Zona</th>
  </tr>

<% @callcenters.each do |callcenter| %>
  <tr>
    <td><%= numero - params[:page].to_i * 30 %><% numero = numero  - 1 %></td>
    <td><%= callcenter.caller_id %></td>
    <td><%= Node.first(:conditions => {:calling_id => callcenter.caller_id}).description %></td>
  </tr>
<% end %>
</table>

, - !

, ivancarrascoq

+4

, `will_paginate-bootstrap '.

  • -, gem, ,

    gem 'will_paginate-bootstrap'.

    .

  • @post = Post.all, .

@post = Post.paginate(:page=>params[:page],per_page:5)

per_page ,   .

  1. index.html.erb , body ,

<%= will_paginate @post,renderer: BootstrapPagination::Rails %>

. , Post , .      .

+1

Ruby on Rails

Ruby on Rails, :

Gemfile:

gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'

Area Controller -> Index

@areas = Area.pagination_request(params[:page])

index.html.erb

<%= will_paginate @areas, renderer: BootstrapPagination::Rails %>

Model File:

def self.pagination_request(page)
    paginate :per_page => 10, :page => page
end
+1
source

You can use stone kaminari.

Very easy to use and easy to set up.

0
source

Source: https://habr.com/ru/post/1723472/


All Articles