2010-09-28 58 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)