Kaminari gem is used to create pagination in Ruby on Rails application.
Step 1: Open the terminal and Create the new application
> rails new simplepage
Step 2: After you create the blog application, switch to its folder:
> cd simplepage
Step 3: Open and Add it to your Gemfile to following line
gem 'kaminari'
Step 4: Run the bundle install.It's used to install the gem dependencies that are already mentioned in Gemfile
> bundle install
Step 5: create the form using scaffolding. Rails scaffolding is a quick way to generate some of the major pieces of an application.(Create, Edit, Delete, Update, Show)
> rails g scaffold student name:string mobile:string city:string
Step 6: Run the migration command.It's used to create the STUDENT table in database.
> rake db:migrate
Step 7: Open and add the following lines to routes.rb file (Location: simplepage/config/routes.rb)
root 'students#index'
# students => controller
# index => function
Step 8: Open and edit the students_controller.rb (Location: simplepage/app/controllers/students_controller.rb)
def index @students = Student.all end
changed to
def index @students = Student.all.page(params[:page]).per(3) end
# per(3) => It's indicates the per page three rows only .
Step 9: Open and add following lines to index.html.erb (Location: simplepage/app/views/students/index.html.erb)
<%= paginate @students %> Like this <tbody> <%= paginate @students %> <% @students.each do |student| %> <tr> <td><%= student.name %></td> <td><%= student.mobile %></td> <td><%= student.city %></td> <td><%= link_to 'Show', student %></td> <td><%= link_to 'Edit', edit_student_path(student) %></td> <td> <%= link_to 'Destroy', student, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </tbody>
Step 10: Run the web Server
> rails server
Step 11 : Open a browser window and navigate to http://localhost:3000

Nice information Ruby on Rails Online Training
ReplyDelete