Use will_paginate for paging and simple query
Run the gem install will_paginate command on the command line. The following result shows that the installation is successful.
Open books_controller.rb (your own Controller)
Comment out all the methods to search. Use the following method to integrate the methods to search by title.
Ruby code
- # @ Books = book. All
- @ Books= Book. paginate: page => Params [: Page],
- : Per_page => 2,
- : Conditions => ["title like? "," % # {Params [: Search]} % "]
- Respond_toDo| Format |
- Format.html # index.html. ERB
- Format. xml {render: xml =>@ Books}
- End
- End
#@books = Book.all@books = Book.paginate :page => params[:page], :per_page => 2, :conditions => ["title like ?", "%#{params[:search]}%"] respond_to do |format| format.html # index.html.erb format.xml { render :xml => @books } end end
Open the corresponding books_controller.rb view page index.html. ERB
Add query function
HTML code
- <% Form_tag books_path,: method =>'Get' do %>
- <P>
- <% = Text_field_tag: search, Params [: Search] %>
- <% = Submit_tag "Search",: Name =>Nil %>
- </P>
- <% End %>
<% form_tag books_path, :method => 'get' do %> <p> <%= text_field_tag :search, params[:search] %> <%= submit_tag "Search", :name => nil %> </p> <% end %>
And paging Functions
HTML code
- <Div>
- <Div >
- <% = Page_entries_info @ books %>
- </Div>
- <% = Will_paginate @ books,: Container =>False %>
- </Div>
<div> <div > <%= page_entries_info @books %> </div> <%= will_paginate @books, :container => false %> </div>
Open the Environment File environment. Rb and add
Ruby code
- Require "will_paginate"
require "will_paginate"
The running effect is as follows:
This article from: http://95700900.javaeye.com/blog/573308