Search code examples
ruby-on-railsrubyruby-on-rails-3link-to

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.


Solution

  • How about

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