Today I found a massy thing on one page of the application that I am working on. The problem is it display all the data that I have in one page. How am I going to change this for user easily use it.
So I found out about pagination in rails. but all the books that I have using rails before 2.0 version. So it won’t work with 2.0.2 version anymore. Because the pagination in rails has moved to be a plug-in now.
This plug-in called “will_paginate” if you don’t have it install in your machine please do this
open the ruby console and type this command
|ruby script/plugin install svn://errtheblog.com/svn/plugins/will_paginate|
After you have this plug-in installed in your machine. So you are ready to go….
Let start with our Model
paginate(:page => page, :per_page => 2)
So in my model I have a method called “display_location_data” or you can change the name to your location (the one that got strickthrough on it)
example my model is user => it’s going to be display_user_data
we receive 1 parameter called “page” to select what page are we look on now.
we have a paginate method that will “display the page that user selected and how many data will be displayed”
Next we move on to our Controller
@display_data = Locationdata.display_location_data (params[:page])
In our controller we just create one method called “display_user” and when ever we want to display our data. we just need to call this method from the action that we want to display the data.
as I said you need to change “Locationdata” (one with strikethrough) to be your model name.
for example my model is “User”. so it would be like this
|@display_data = User.display_user_data (params[:page])|
And the last part is View
|<%= will_paginate @display_data %>|
we just need to add this tag in our view page. that’s it for Easy pagination.
NOTE ok one more thing you can use that variable in this example is “@display_data” to render data. because that variable already contain the data that’s going to view on that page already.
Created by “WhenURnotAround”
Thanks for http://nasir.wordpress.com