[英]Non-standard routes in Rails
我目前有一個如下所示的route.rb文件:
map.resources :profiles do |profile|
profile.resources :projects, :has_many => :tasks
end
這給了我這樣的路線:
/profiles/:profile_id/projects/:project_id/tasks
這接近於我想要的,但我不想使用“ / profiles /:profile_id /”部分來代替它,所以路由看起來像這樣:
/:profile_user/projects/:project_id/tasks
我怎樣才能實現這樣的目標? 我四處張望,卻未找到任何有關如何執行此操作的信息,但我可能也沒有一直在尋找正確的東西。
我使用命名空間或連接嘗試了一些選項,但沒有成功。
如果您確實要執行這些路由,我認為您必須使用connect並創建所有路由,如下所示:
map.connect ':profile_user/projects/:project_id/tasks', :controller => :tasks, :action => :index, :method => :get
map.connect ':profile_user/projects/:project_id/tasks/new', :controller => :tasks, :action => :new, :method => :get
map.connect ':profile_user/projects/:project_id/tasks', :controller => :tasks, :action => :create, :method => :post
您可以使用with_options方法:
map.with_options(:path_prefix => ":profile_user", :name_prefix => "profile_" ) do |profile|
profile.resources :projects, :has_many => :tasks
end
然后它為您提供如下路線:
profile_project_tasks_path(user.username, project)
# => /:profile_user/projects/:project_id/tasks
new_profile_project_task_path(user.username, project)
# => /:profile_user/projects/:project_id/tasks/new
等等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.