发表时间:2007-03-13
最后修改:2008-11-15
如果你是用过Google的搜索条,就会知道,它会根据你的输入实时的列出相关的搜索项目。让我们在自己的 Rails 网站里也添加这一功能。
1.
新建application
创建一个 controller images,它包含三个 action。再创建一个 model image,要有个字段为 filename。
ruby 代码
- rails searchdemo
- cd searchdemo
- ruby script/generate controller images index suggest you_choose
- ruby script/generate model image
2. search.xml
在 public 目录下创建 search.xml。
URL-TO-AN-ICON-FILE要换成你自己网站icon文件的URL。
YOUR-ADDRESS换成你自己的网址。
xml 代码
- xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
- <
OpenSearchDescription
xmlns
=
"http://a9.com/-/spec/opensearch/1.1/"
- xmlns:moz
=
"http://www.mozilla.org/2006/browser/search/"
>
- <
ShortName
>
Image Search
<!---->ShortName
>
- <
Description
>
Image Search
<!---->Description
>
- <
InputEncoding
>
UTF-8
<!---->InputEncoding
>
- <
Image
height
=
"16"
width
=
"16"
type
=
"image/x-icon"
>
URL-TO-AN-ICON-FILE
<!---->Image
>
- <
Url
type
=
"application/x-suggestions+json"
template
=
"http://YOUR-ADDRESS/images/suggest?filename={searchTerms}"
/>
- <
Url
type
=
"text/html"
method
=
"GET"
template
=
"http://YOUR-ADDRESS/images/you_choose/{searchTerms}"
/>
- <!---->OpenSearchDescription
>
3.
Controller
rhtml 的内用就省略了。
ruby 代码
- def
suggest
- headers["Content-Type"
] =
"application/x-suggestions+json"
- codes = Image.find(:all
,
:conditions
=>[
"filename LIKE ?"
,params[
:filename
]+
"%"
],
:limit
=>10,
:group
=>
"filename"
,
:order
=>
"filename"
)
- @res
= codes.map{
|c
| c.filename }
- render :text
=> [params[
:filename
],
@res
].to_json
- end
-
- def
you_choose
- @image_pages
,
@images
= paginate
:images
,
:per_page
=> 20,
- :order
=> 'filename',
- :conditions
=> [
"filename LIKE ?"
,params[
:filename
]+
"%"
]
- end
4. Settings
layouts/images.rhtml 文件中,追加下面的 link。
xml 代码
- <
link
rel
=
"search"
type
=
"application/opensearchdescription+xml"
title
=
"Search a Image"
href
=
"http://YOUR-ADDRESS/search.xml"
/>
routes.rb 里,追加下边的映射。
ruby 代码
- map.connect '
:controller
/
:action
'
5. 进阶
想上边那样设定的话,已经为网站追加了OpenSearch功能。可是当测试网址和实际网址不同的时候,xml 文件里的静态URL就需要修改。让我们用 url_for 将它改进。
追加一个 action opensearch。
ruby 代码
- def
opensearch
- headers["Content-Type"
] =
"application/opensearchdescription+xml; charset=utf-8"
- render :layout
=>
false
- end
opensearch.rhtml (当然,search.xml要删除)
xml 代码
- <
OpenSearchDescription
xmlns
=
"http://a9.com/-/spec/opensearch/1.1/"
- xmlns:moz
=
"http://www.mozilla.org/2006/browser/search/"
>
- <
ShortName
>
Image Search
<!---->ShortName
>
- <
Description
>
Image Search
<!---->Description
>
- <
InputEncoding
>
UTF-8
<!---->InputEncoding
>
- <
Image
height
=
"16"
width
=
"16"
type
=
"image/x-icon"
>
<
%= @request.protocol + @request.host_with_port %
>
/images/image.ico
<!---->Image
>
- <
Url
type
=
"text/html"
method
=
"get"
template
=
"<%= url_for :only_path => false, :controller => 'images', :action => 'you_choose' %>"
>
- <
Param
name
=
"filename"
value
=
"{searchTerms}"
/>
- <!---->Url
>
- <
moz:SearchForm
>
<
%= url_for
:only_path
=
>
false,
:action
=
>
'list',
:controller
=
>
'images' %
>
<!---->moz:SearchForm
>
- <
Url
type
=
"application/x-suggestions+json"
template
=
"<%= url_for :only_path => false, :action => 'suggest', :controller => 'images' %>?filename={searchTerms}"
/>
- <!---->OpenSearchDescription
>
layouts/images.rhtml 文件中的 link 也要修改。
xml 代码
- <
link
rel
=
"search"
type
=
"application/opensearchdescription+xml"
title
=
"Search a Image"
href
=
"<%= url_for :only_path => false, :controller => 'images', :action => 'opensearch' %>"
>