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: