John Bachir
John Bachir

Reputation: 22721

URL params for polymorphic url_for

I currently have this code:

link_to 'add a new baz!', new_foo_bar_baz_path(@foo, @bar, baz: { thing: 'the-value' })

which generates this url:

http://ganxy.local/foos/1/bars/2/bazes/new?baz%5Bthing%5D=the-value

Because of some changes I'm making to my app I want to make the url generation polymorphic. So, something like this:

link_to 'add a new baz!', [@foo, @bar, :baz]# : { thing: 'the-value' })

Is there a way to add the ?baz%5Bthing%5D=the-value to the end, or do I need to generate the string manually?

Upvotes: 1

Views: 1233

Answers (3)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

You can do this:

link_to 'add a new baz!', [[@foo, @bar, :baz], thing: 'the-value']

Upvotes: 1

ka8725
ka8725

Reputation: 2918

I wouldn't recommend to use polymorphic url because it 2x slower than helper method. Check out my post about this

Upvotes: 2

Dmitry Lihachev
Dmitry Lihachev

Reputation: 504

See Rails 3 - Nested resources and polymorphic paths: OK to two levels, but break at three

So you can write

link_to 'add a new baz!', polymorphic_url([@foo, @bar, :baz], thing: 'the-value')

Upvotes: 1

Related Questions