7

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.

1

2 Answers 2

10

Instead eval use public_send

TITLES.each do |t|
  = link_to t, public_send("#{t}_super_users_path", user)
Sign up to request clarification or add additional context in comments.

1 Comment

public_send("#{t}_super_users_path",user)
7

How about

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

1 Comment

i wouldn't recommend using eval ever, just google it. lots of bad results can happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.