Printing the alphabet easily in Ruby on Rails
It's really easy to print out the alphabet with Ruby. I needed to do recently as an additional method for searching by letter. Although, I could see it might be useful for pagination.
The code I used is as follows:
<ul>
<% "A".upto("Z") do |l| -%>
<li><%= link_to l, genre_letter_url(:letter => l) %></li>
<% end %>
</ul>
The key bit is "A".upto("Z") do |l|. Note that if you use lowercase letters then your output will be in lowercase.
If you are wondering about my use of genre_letter_url(:letter => l) This is best explained in my article about routes in ruby on rails and in particular named routes.
About
Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.
Contact
my name at gmail.com
More snippets
Take a look in the archive
Need a website?
Contact my employer. Make sure to check out our portfolio of work.
Hosting
I recommend hostingrails.com
2 comments made
i like better
Both works. Nice help buddies
Got something to say?