2013-06-02 91 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