2010-09-28 37 views
8

我喜欢多态性风格的URL非常多,所以我可以写轨多态性网址参数

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

,而不是

link_to 'New taste', new_taste_path 

但是,它可以添加查询参数到第一个不使用polymorphic_url/polymorphic_path

+0

据我所知你不能。 – vise 2010-09-28 21:24:38

+0

你的意思是'link_to'New taste',[:new,:taste],:a_param =>'param''是不可能的? – Yannis 2010-09-28 21:33:05

+0

@Yannis:不,这会将锚点标记的属性添加到 – tig 2010-09-28 22:28:39

回答

3

是 - 你可以把它像这样:

link_to 'New taste', [[:new, :taste], :a_param => 'param'] 
+1

似乎它仅在rails中可用〜> 4.1 – tig 2014-11-05 22:35:01

8

号当传递数组于这些方法(link_toredirect_to等)url参数被直接传递到url_for,它本身具有一个参数调用polymorphic_path。正如您所说polymorphic_path允许params作为第二个参数传递,但它们不能像第一个参数那样传递。

为了通过PARAMS和使用多态路由,你必须使用polymorphic_path/polymorphic_url像这样:

link_to 'New taste', polymorphic_path([:new, :taste], :spiciness => :on_fire)