---
title: "ruby on rails 实现按首字母搜索。A..Z"
author: "PBFox"
date: "2007-09-12T11:08:00Z"
tags: []
slug: "ruby-on-rails-a-z"
draft: false
lastmod: "2007-09-12T11:08:00Z"
id: 479292
url: "https://developerwork.iteye.com/blog/479292"
---

helper:

def alphabet\_index(url)  
output=""  
('A'..'Z').each do |alpha|  
output &lt;&lt; &lt;&lt;-EOF  
#{link\_to\_remote(alpha, :update=&gt;'table',:url=&gt;{:action=&gt;url,:id=&gt;alpha})}  
EOF  
end  
output  
end

controller:

def sort\_alphabet  
alpha=params\[:id]  
condition=\['title LIKE ? "%"',alpha]  
@materials=Material.find(:all,:conditions=&gt;condition)  
if request.xml\_http\_request?  
render(:partial=&gt;"materials\_list",:layout=&gt;false)  
end  
end

view:

&lt;div class="alphabet"&gt;&lt;%=alphabet\_index("sort\_alphabet")%&gt;&lt;/div&gt;