Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails polymorphic url params

I like polymorphic style url very much, so I can write

link_to 'New taste', [:new, :taste]

instead of

link_to 'New taste', new_taste_path

But is it possible to add query parameters to first one without using polymorphic_url/polymorphic_path?

like image 561
tig Avatar asked Sep 28 '10 18:09

tig


3 Answers

No. When you pass an Array to these methods (link_to, redirect_to, etc) the url argument is passed directly to url_for, which itself calls polymorphic_path with a single argument. As you stated polymorphic_path allows params to be passed as the second argument, but they cannot be passed as in the first argument.

In order to pass params and use polymorphic routing you have to use polymorphic_path / polymorphic_url like so:

link_to 'New taste', polymorphic_path([:new, :taste], :spiciness => :on_fire)
like image 118
Luca Spiller Avatar answered Sep 22 '22 19:09

Luca Spiller


Yes - you can pass it like so:

link_to 'New taste', [[:new, :taste], :a_param => 'param']
like image 23
Mark Swardstrom Avatar answered Sep 22 '22 19:09

Mark Swardstrom


I think it is more this syntax on Rails >= 4.2:

link_to 'New taste', [:new, :taste, { a_param: 'param' }]
like image 43
paco Avatar answered Sep 22 '22 19:09

paco