A Neat RubyOnRails Profile Link Helper

I am working on a project for my client and they have a movie and an associated list of actors. When viewing a movie, they want to be able to list all the actors starring in it, obviously.

1
2
3
def link_to_actors(actors, options = {})
  actors.map{ |a| link_to(a.full_name, actors_path(a), options) }.to_sentence
end

This uses map to iterate over each actor, link to their profile, pass any options we want, then assemble with to_sentence. To use it is extremely simple.

1
<%= link_to_actors(@movie.actors, :class => 'actor') %>

Simple, as it should be.

Related posts:

  1. Find Missing Table Indexes With ActiveRecord For The Inept
  2. Nifty RubyOnRails Layout Method
  3. Why MemCache Makes Me Want To Spoon Out My Own Eyes
  • Kung fu + 1. I used a very similar approach on a few projects, and I like this better - keeps things nice and clean.
blog comments powered by Disqus