auto complete plugin

简介:
[url]http://fsjoy.blog.51cto.com/318484/99823[/url]文中提到的这种情况:
可以使用auto complete插件来实现
 
这里还通过一个实例来说明:
说明:环境 rails 2.0.2, mysql, winxp
 
--有两个模型类:
Author:
#Class 

class Author < ActiveRecord::Base 
    has_many :articles 
end 



#Migration 


class CreateAuthors < ActiveRecord::Migration 
    def self.up 
        create_table :authors do |t| 
            t.column :name, :string 
        end 
    end 

    def self.down 
        drop_table :authors 
    end 
end 


Article:
#Class 

class Article < ActiveRecord::Base 
    belongs_to :author 
end 

#Migration 

class CreateArticles < ActiveRecord::Migration 
    def self.up 
        create_table :articles do |t| 
            t.column :author_id, :integer 
            t.column :content, :text 
        end 
    def self.down 
        drop_table :articles 
    end 
end
对artilces资源的edit模板:
<h1>Edit</h1> 
<%form_for(:article, :url=>article_path(@article), :html=>{:method=>:put}) do |f| %> 
    <p> 
        <label>Content:</label> 
        <%=f.text_field :content %> 
    </p> 
    <p> 
        <label>Author: </label> 
         <%=f.collection_select :author_id, Author.find(:all), :id, :name %> 
    </p> 
    <%= submit_tag "submit"    %> 
<%end%> 
这里使用了选择框:
---这里我们希望做点改变,将author这个选择框变成一个输入框,输入的内容在authors表中做模糊查询,得到的结果在下面列出来。或者可以输入原本authors中不存在的数据,保存之后会添加到authors表中。
 
修改edit模板:
<h1>Edit</h1> 
<%form_for(:article, :url=>article_path(@article), :html=>{:method=>:put}) do |f| %> 
    <p> 
        <label>Content:</label> 
        <%=f.text_field :content %> 
    </p> 
    <p> 
        <label>Author: </label> 
        <%=f.text_field :author_name %> 
    </p> 
    <%= submit_tag "submit"    %> 
<%end%> 
这里的author_name在articles表中并没有定义,所以在Article类中定义一个虚拟属性:
 
class Article < ActiveRecord::Base 
    belongs_to :author 
     #读方法,如果author存在的话,那么author.name值就是author_name
     def author_name 
        author.name if author
    
end 
     #写方法,首先在authors表中查找name字段匹配的数据,存在,那么跟arti#cle.author发生关联,查找不到,那么创建新的author对象。
     def author_name=(name) 
        self.author=Author.find_or_create_by_name(name) unless name.blank? 
    end 
end 
现在就可以实现输入保存的功能了,但是还没有做自动完成的功能
 
下面安装auto complete插件
>ruby script/plugin install auto_complete
因为auto complete功能需要使用ajax,所以在layout/application视图中应该声明
<%= javascript_include_tag :defaults    %>
那么如何通过ajax来让author输入框将name字段列出来呢?
应该是这样,在author输入框中的内容传到controller中,执行一个类似于这样的查询:
@authors = Author.find(:all, :conditions=>["name LIKE ?", "%#{params[search]}%"])
得到的这个@authors实例变量显示在author输入框下面
 
好了,来看看怎么实现这个功能:
在authors资源中,index这个action中的代码:
class AuthorsController < ApplicationController 
    def index 
        @authors=Author.find (:all, :conditions=>["name like ?", "%#{params[:search]}%"], :include=>[:articles]) 
    end 
end
我们希望能将输入文本框中的字符通过ajax方式,将其放到params[:search]中,然后在author输入框下面有一个下拉框一样的东西,显示的是@authors变量的name字段。
 
将edit.erb的内容修改如下:
 
<h1>Edit the Article</h1>    
<%form_for(:article, :url=>article_path(@article), :html=>{:method=>:put}) do |f| %>    
        <p>    
                <label>Content:</label>    
                <%=f.text_field :content %>    
        </p>    
        <p>    
                <label>Author: </label>    
<%= text_field_with_auto_complete :article, :author_name, {},{:url=>formatted_authors_path(:js), 
  :method=>:get, :with=>"'search='+element.value" }%>

        </p>    
        <%= submit_tag "submit"        %>    
<%end%>    
 
效果图如下:
 
值得注意的是这里表出来的一行
<%= text_field_with_auto_complete :article, :author_name, {},{:url=>formatted_authors_path(:js),    
    :method=>:get, :with=>"'search='+element.value" }%>
text_field_with_auto_complete是因为装了auto_complete插件之后可以使用这个tag
 
后面跟的参数第一个article是类名,第二个author_name是属性名,第三个{}里面可以写text_field的size属性等,第四个说明auto_complete显示内容,get方法,给params[:search]参数赋值。
 
OK..在rails1.2上自带有auto_complete这个helper,具体内容可以baidu或者google。。




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/108526,如需转载请自行联系原作者
目录
相关文章
|
28天前
|
JavaScript 编译器
成功解决:Module build failed: Error: Vue packages version mismatch
这篇文章记录了解决Vue项目中遇到的"Module build failed: Error: Vue packages version mismatch"错误的步骤,原因是项目中Vue依赖的版本不一致,解决方法是删除`node_modules`后重新安装指定版本的Vue和`vue-template-compiler`,确保版本匹配,最终成功运行项目。
成功解决:Module build failed: Error: Vue packages version mismatch
|
4月前
|
缓存 Java 开发工具
Error:Execution failed for task ‘:app:preDebugAndroidTestBuild’. Conflict with dependency ‘com.andr
Error:Execution failed for task ‘:app:preDebugAndroidTestBuild’. Conflict with dependency ‘com.andr
23 1
|
4月前
|
Oracle 关系型数据库
The opatch minimum version check for patch failed
The opatch minimum version check for patch failed
33 2
|
10月前
Error starting ApplicationContext. To display the auto-configuration report re-run your application
Error starting ApplicationContext. To display the auto-configuration report re-run your application
Fullpage.js version 3 has changed its license to GPLv3 and it requires a `licenseKey` option ...
Fullpage.js version 3 has changed its license to GPLv3 and it requires a `licenseKey` option ...
124 0
|
Java Linux
error: Failed dependencies: libjvm.so()(64bit) is needed by (installed)
error: Failed dependencies: libjvm.so()(64bit) is needed by (installed)
error: Failed dependencies: libjvm.so()(64bit) is needed by (installed)
Execution failed for task :Test:lintVitalRelease/Lint found fatal errors while assembling a release
Execution failed for task :Test:lintVitalRelease/Lint found fatal errors while assembling a release
116 0
No plugin found for prefix ‘doclint‘ in the current project
No plugin found for prefix ‘doclint‘ in the current project
117 0
configure: error: C compiler cannot create executables
configure: error: C compiler cannot create executables
311 0
|
网络协议 关系型数据库 Linux
onfigure: error: no acceptable C compiler found in $PATH See `config.log' for more details 问题解决
onfigure: error: no acceptable C compiler found in $PATH See `config.log' for more details 问题解决
176 0