2013-01-16 44 views
0

我在routes.rb中寫這樣的:如何在Ruby on Rails的URL中添加參數?

get 'app/:name&:target' => 'apps#show', :as => :app_info 

我想訪問 「應用程序/ MyApp的& mytarget」,然後去apps_controller#秀,這是HAML:

link_to app_item[:name], app_info_url(app_item[:name], app_item[:target]) 

但它返回:

No route matches [GET] "/app/myapp&mytarget" 

我在這裏錯過了什麼?

+0

爲什麼「&」的查詢字符串''據我所知也許開始你?想要像這樣獲得'app /:name /:target' – Viren

回答

2

您不需要指定您希望在路由中接受的URL參數。只需創建PARAMS內到控制器#行動路線,如)

get 'app/:name' => 'apps#show', :as => :app_info 

和您的應用程序#show動作訪問內部的URL參數,從[:目標],可以有許多這樣的URL參數,他們都將在rails爲你生成的params哈希中。您不必在路線中明確提及它們。

所以只是傳遞參數到您的網址助手:

link_to app_item[:name], app_info_url(app_item[:name], target: app_item[:target]) 
0

通一樣,在路徑或URL

app_info_url(app_item[:name], app_item[:target], :params1 => "value1", params2 => "value2"......) 
相關問題