ruby on rails 实现按首字母搜索。A..Z
helper:
def alphabet_index(url)
output=""
('A'..'Z').each do |alpha|
output << <<-EOF
#{link_to_remote(alpha, :update=>'table',:url=>{:action=>url,:id=>alpha})}
EOF
end
output
end
controller:
def sort_alphabet
alpha=params[:id]
condition=['title LIKE ? "%"',alpha]
@materials=Material.find(:all,:conditions=>condition)
if request.xml_http_request?
render(:partial=>"materials_list",:layout=>false)
end
end
view:
<div class="alphabet"><%=alphabet_index("sort_alphabet")%></div>