shitou's blog 
Home About Feed | MIX BOY 塌客

Tags "auto complete"

29
May

插件auto_complete的使用 by shitou

0

因为项目需要,找个下rails上的ajax自动填充表单的插件,在agilewebdevelopment上找到了auto_complete
上面说这个插件是官方的插件,应该不错
先用ruby script/plugin install auto_complete安装
然后就是使用了

  1.首先在一Controller中加入
    auto_complete_for :report, :title
    :report是对应的model名, title是字段名
  2.在控制器中定义一action
     def auto_complete_for_report_title
      search = params[:report][:title]
      @articles = Report.find(:all, :conditions => ["title like ?", "%#{search}%"], :limit => 10, :order => "id DESC") unless search.blank?
      render :partial => "live/search"
     end

   action的命名要以auto_complete_for_开始,然后是对应上面定义的auto_complete_for :report, :title
  3.然后在view中调用就行了,记得先调用rails的default js文件
    <% form_tag '/welcome/auto_complete_for_report_title' do %>
      <%= text_field_with_auto_complete :report, :title, { :size => 35, :value => '' } %>
    <% end %>

    其中value是指默认文本框的value值为空,因为我在测试时发现每次代开页面时文本框的value值会自动与当前页面模型的title进行绑定显示,这样显然不行的,所以就加了这条属性,让它每次都自动为空
   4.生成的模板文件会自动创建,在views/live/_search.rhtml中,根据自己的需要定制就行
   5.然后打开页面测试时报错,说是表单认证错误,后来google了下才知道,rails2.0后为了防止外部调用表单,进行数据库的操作,加了此项安全措施,只用在application.rb中加入
   protect_from_forgery :only => [:create, :update, :destroy]
   就行了
   6.样式可以自定义,打开页面后,查看下源代码,就知道定义那些属性了,可以覆盖的

Tags: rails,plugin,auto complete

2008-05-29 01:08:45, 606 reviews

send to mailbox

Your email:

Tags

U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • iPhone[17]
  • Erlang[4]
  • google[8]
  • 生活[38]
  • 音乐[11]
  • 电影[11]
  • linux[20]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[5]
  • ruby[18]
  • ruby on rails[27]
  • 开源[3]

Episode

  • iPhoneException
  • shell
  • thread
  • memcache

Recent Comments

  • comment2, xenical canada, 5528, prilosec packag...
  • comment3, nolvadex package insert, 2877, xenica...
  • comment2, http://www.freecodesource.com/user/pr...
  • comment6, http://www.freecodesource.com/user/pr...
  • comment3, xenical support, :'-), prilosec half ...
  • comment7, how many hydrocodone does it take to ...
  • comment3, oxycodone acetaminophen, :E, viagra d...
  • comment7, http://www.freecodesource.com/user/pr...
  • comment6, valium for pain, accompanying, xanax ...
  • comment4, oxycodone ingredients, 2732, buy viag...

Popular Posts

  • MySQL Innodb备份
  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Lighttpd配置参数
  • iPhone上的HelloWorld终于跑起来了
  • Etag和Expire

Recommended Posts

  • Mai Kuraki -永远より ながく
  • U-ka saegusa IN db Final Best
  • Heaven Can Wait - Charlotte Gainsbou
  • Ruby遍历MemCached的key
  • Norah Jones - The fall
  • 请记得仰望梦想的姿势
  • Shell: 统计MySQL InnoDB表的大小
  • Rails Benchmark
  • 发送异常到邮箱
  • I Miss Nobody
  • Music4u, Vol. 1
  • my macbook
  • Mai Kuraki-Beautiful
  • 10首最伤情英文歌曲精选
  • Mai Kuraki - PUZZLE/Revive

Friends' blogs

  • levy
  • sphance
  • andreas

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 51170
  • 今天访问: 34
  • 日志: 172
  • 评论: 117
  • 音乐: 9
  • 用户: 149


 

just DO NOT support IE

close