Ruby on Rails step by step (2) paging and simple query

Source: Internet
Author: User

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
  1. # @ Books = book. All
  2. @ Books= Book. paginate: page => Params [: Page],
  3. : Per_page => 2,
  4. : Conditions => ["title like? "," % # {Params [: Search]} % "]
  5. Respond_toDo| Format |
  6. Format.html # index.html. ERB
  7. Format. xml {render: xml =>@ Books}
  8. End
  9. 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
  1. <% Form_tag books_path,: method =>'Get' do %>
  2. <P>
  3. <% = Text_field_tag: search, Params [: Search] %>
  4. <% = Submit_tag "Search",: Name =>Nil %>
  5. </P>
  6. <% 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
  1. <Div>
  2. <Div >
  3. <% = Page_entries_info @ books %>
  4. </Div>
  5. <% = Will_paginate @ books,: Container =>False %>
  6. </Div>
 <div>      <div >        <%= page_entries_info @books %>      </div>      <%= will_paginate @books, :container => false %>    </div>

 

Open the Environment File environment. Rb and add

 

Ruby code
  1. Require "will_paginate"
require "will_paginate"

 

The running effect is as follows:

 

 

This article from: http://95700900.javaeye.com/blog/573308

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.