ruby on rails Ò»²½²½¿ªÊ¼(2)·ÖÒ³ºÍ¼òµ¥²éѯ
ʹÓà will_paginate ½øÐзÖÒ³ºÍ¼òµ¥²éѯ
ÔÚÃüÁîÐÐÏÂʹÓà gem install will_paginate ÃüÁ³öÏÖÏÂÃæ½á¹û°²×°³É¹¦
´ò¿ª books_controller.rb (Äã×Ô¼ºµÄ¿ØÖÆÆ÷)
×¢Ê͵ô²éÕÒÈ«²¿µÄ·½·¨£¬Ê¹ÓÃÏÂÃæµÄ·½·¨£¬ÒѾ¼¯³É¸ù¾Ýtitle½øÐвéѯ
Ruby´úÂë
#@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 = 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
Ìí¼Ó²éѯ¹¦ÄÜ
Html´úÂë
<% form_tag books_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_ta
Ïà¹ØÎĵµ£º
¡¡×î½ü£¬ÎÒÐèÒªÒ»ÖÖ¿ìËÙ¿ª·¢ÓïÑÔ£¬Ä¿Ç°ÖËÊÖ¿ÉÈȵĿìËÙ¿ª·¢ÓïÑÔ¾ÍÊÇPythonºÍRubyÁË¡£RubyºÍPython¶¼ÊǷdz£ÓÅÐãµÄ½Å±¾ÓïÑÔ£¬ËäÈ»ËûÃǵÄÉè¼ÆÕÜѧ¼¸ºõÍêÈ«²»Í¬£¬µ«ËûÃÇЧÂʲ¶à£¬Ó¦ÓÃÁìÓòÒ²²î¾à²»´ó£¬ËƺõÌìÉú¾ÍÊǾºÕù¶ÔÊÖ¡£µ½µ×Ñ¡ÄǸöÄØ?ÎÒ×öÁËһЩ¼òµ¥µÄ±È½Ï¡£
¡¡¡¡ÎÒÃDZȽ ......
·ÖÒ³ÖÐÓõ½Àà±äÁ¿£¬Ö÷ÒªÊÇÓÃÀ´±ê¼Ç¡°Ò³ÂëÊäÈë¿ò¡±µÄid Èç¹ûÒ»¸öÒ³ÃæÓм¸¸ö·ÖÒ³£¬¡°Ò³ÂëÊäÈë¿ò¡±µÄidÒªÊDz»Í¬µÄ²ÅÄÜ·ÖÇåÊÇÄĸöÒª·ÖÒ³¡£Ê¹ÓÃÀà±äÁ¿¾ÍÊÇΪÁË´ïµ½Õâ¸öÄ¿µÄ£¬ÈÃËùÓеĶÔÏóʵÀý¹²ÓÃÒ»¸ö±äÁ¿£¬²»±Øÿ´ÎÖØгõʼ»¯±äÁ¿¡£ Àà±äÁ¿Ê¹ÓôúÂëʾÀý 1 require 'ruby-debug'
2 debugger
3 cla ......
¿ª·¢»·¾³
OS£ºWindowsXP
Ruby: Ruby1.9.1
Rails: Rails2.3.5
IDE: RubyMine2.0.1
1¡¢´´½¨Rails¹¤³Ì
2¡¢ÐÞ¸Ä /config/database.yml
×Ô¶¯´´½¨µÄ¹¤³ÌÖÐĬÈÏÊý¾Ý¿âÁ¬½ÓµÄÊÇsqlite£¬Èç¹ûûÓа²×°´ËÊý¾Ý¿â£¬ÐèÒªÐ޸ĸÃÅäÖ㨱¾ÀýÖÐʹÓõÄÊÇmysql£©
# Mysql Version 5.1.46
development:
adapter: mysql
database: ......
¿ª·¢»·¾³£º
Ruby:1.9.1
Rails:2.3.5
Rake:0.8.7
Rack:1.0.1
Mysql:5.0.9
Ruby-mysql:mysql-2.8.1-x86-mswin
IDE:RubyMine2.0.1
Êý¾Ý¿â×¼±¸£º
database:dbdevelopment
user:crystal
password:crystal
Ò»¡¢´´½¨RubyÏîÄ¿RorTest
¶þ¡¢ÐÞ¸Ädatabase.yml
ÕâÀïÖ»ÆôÓÃdevelopment»·¾³Êý¾Ý¿â£¬ÐÞ¸ÄÅäÖÃÎļþÈçÏ£º
dev ......