The path from the string with the parameter

I need a good way to create a Rails 3 path set from an array in an assistant link_to.

I have:

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

Thus, I need to build 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 the same _super_users prefix for each individual path and pass the custom object. As a 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.

+5
source share
2 answers

What about

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

Instead, eval uses public_send

TITLES.each do |t|
  = link_to t, public_send("#{t}_super_users_path", user)
+4
source

All Articles