Search code examples
ruby-on-railsrubyhaml

Ruby on Rails: Use Variable in link_to helper path


I've got a HAML partial that receives a variable bar, and I'd like to inject that variable in the link_to path.

For example:

= link_to new_foo_path, class: 'source card' do
    .stuff

I want to replace foo with bar.

I've tried:

= link_to new_#{bar}_path, class: 'source card' do

and a dozen other things, but nothing seems to work.

Thoughts?


Solution

  • You can try it like this:

    link_to send("new_#{bar}_path"), class: "source card" do

    Basically, send makes something a method or variable, and it allows you to combine everything in that string into one variable.