6

I need a good way to create a set of Rails 3 paths from an array, in the link_to helper.

I have:

TITLES = ['foo', 'bar', 'baz']
TITLES.each do |t|
  = link_to t, (.....path....)

This way i need to construct a set of paths:

foo_super_users_path(user)
bar_super_users_path(user)
baz_super_users_path(user)

As you can see, i need to add same prefix _super_users for every single path, and pass user object. As the final result, i need something like:

link_to t, foo_super_users_path(user)
link_to t, bar_super_users_path(user)
link_to t, baz_super_users_path(user)

Your suggestions are really appreciated.

Rich
  • 5,603
  • 9
  • 39
  • 61
Leo Burt
  • 109
  • 1
  • 6
  • This maybe similar to what you are looking for: http://stackoverflow.com/questions/2792946/how-do-i-dynamically-change-the-path-to – Chong Yu Apr 16 '13 at 17:43

2 Answers2

9

Instead eval use public_send

TITLES.each do |t|
  = link_to t, public_send("#{t}_super_users_path", user)
Michal Macejko
  • 370
  • 4
  • 8
7

How about

TITLES.each do |t|
  = link_to t, eval("#{t}_super_users_path(user)")