2013-06-02 93 views
2

如何獲得指定路線的路徑助手?紅寶石軌道如何獲得指定路線的路徑

的routes.rb

match 'report/monitor_id/:monitor_id/week_ending_date/:week_ending_date' => 'report#index' 

如何獲得命名的路由路徑幫手?當我做耙路線,沒有什麼前

/report/monitor_id/:monitor_id/week_ending_date/:week_ending_date(.:format)  report#index 

有沒有辦法讓report_monitor_id_week_ending_date_path(monitor_id,week_ending_date)?

回答

5

你可以給它一個名字與:as參數:

http://guides.rubyonrails.org/routing.html#naming-routes

例:

match 'exit' => 'sessions#destroy', :as => :logout 

應提供的助手:

logout_path 
logout_url 

不知道你想要你的路線被命名,但可能類似:

match 'report/monitor_id/:monitor_id/week_ending_date/:week_ending_date' => 'report#index', :as => :weekly_monitor_report 

我相信這會給你允許傳遞順序參數它們在路由定義中指定的助手:

weekly_monitor_report_path(:monitor_id, :week_ending_date) 
weekly_monitor_report_url(:monitor_id, :week_ending_date) 
+0

的:這將是擺在url;例如http:// localhost:3000/weekly_monitor_report而不是http:// localhost:3000/report/monitor_id/xxx/week_ending_date/yyy。 看起來我可以使用嵌套資源 ** ** route.rb '資源:報告做 資源:monitor_ids做 資源:week_ending_dates做 資源:報告 結束 結束 結束 ' – user2371769