Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 308
brianrhea Avatar asked Aug 27 '15 15:08

brianrhea


2 Answers

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.

like image 138
Steph Rose Avatar answered Oct 10 '22 04:10

Steph Rose


You can add parameters to the link helper like this:

link_to "New Foo", new_foo_path(bar: "bar")
like image 22
j-dexx Avatar answered Oct 10 '22 06:10

j-dexx