Leo Burt
Leo Burt

Reputation: 109

Path from string with parameter

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.

Upvotes: 6

Views: 7460

Answers (2)

Michal Macejko
Michal Macejko

Reputation: 370

Instead eval use public_send

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

Upvotes: 9

user946611
user946611

Reputation:

How about

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

Upvotes: 7

Related Questions