作者:mobiledu2502857893 | 来源:互联网 | 2023-10-11 15:05
使用will_paginate进行分页和简单查询在命令行下使用geminstallwill_paginate命令,出现下面结果安装成功打开books_control
使用 will_paginate 进行分页和简单查询
在命令行下使用 gem install will_paginate 命令,出现下面结果安装成功
打开 books_controller.rb (你自己的控制器)
注释掉查找全部的方法,使用下面的方法,已经集成根据title进行查询
-
- @books = Book.paginate :page => params[:page],
- :per_page => 2,
- :conditions => ["title like ?", "%#{params[:search]}%"]
- respond_to do |format|
- format.html
- 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
打开对应的 books_controller.rb 视图页面 index.html.erb
添加查询功能
- <% form_tag books_path, :method &#61;> &#39;get&#39; do %>
- <p>
- <%&#61; text_field_tag :search, params[:search] %>
- <%&#61; submit_tag "Search", :name &#61;> nil %>
- p>
- <% end %>
<% form_tag books_path, :method &#61;> &#39;get&#39; do %>
<%&#61; text_field_tag :search, params[:search] %>
<%&#61; submit_tag "Search", :name &#61;> nil %>
<% end %>
及分页功能
- <div>
- <div >
- <%&#61; page_entries_info &#64;books %>
- div>
- <%&#61; will_paginate &#64;books, :container &#61;> false %>
- div>
<%&#61; page_entries_info &#64;books %>
<%&#61; will_paginate &#64;books, :container &#61;> false %>
打开环境文件 environment.rb 在end后添加
require "will_paginate"
运行效果如下
本文转自:http://95700900.javaeye.com/blog/573308