带有空路径和助手的路由
Route with empty path and helper
我正在尝试实施类似 "github.com/josevalim/inherited_resources" 的“à la github”url。
我用这段代码实现了我的目标:
resources :users, :path => '', only: [] do
resources :projects, :path => '', only: [:show]
end
但是现在,我找不到正确重定向到此路由的方法。
当我在控制台中 运行 一个 rake routes
时,我只有
GET /:user_id/:id(.:format) projects#show
没有找到像我的旧 users_project_path
.
这样的辅助方法的名称
我错过了什么吗?谢谢!
你删除路径是因为这个原因你可以得到route_path,用as建立路由名。
resources :users, :path => '', only: [] do
get :projects, "/:user_id/:id", :to => "project#show", :as => :project_user
end
或者如果您不想使用,您可以使用此代码生成路径:
url_for([@user, @project])
我最近做了类似的事情,试试下面的方法
scope '/:user_id', as: :user do
resources :projects, path: '', only: [:show]
end
运行 rake routes
应该给你
user_project GET /:user_id/:id(.:format) projects#show
这将为您提供合适的 user_project_path(:user_id, :id)
路由助手。
我正在尝试实施类似 "github.com/josevalim/inherited_resources" 的“à la github”url。
我用这段代码实现了我的目标:
resources :users, :path => '', only: [] do
resources :projects, :path => '', only: [:show]
end
但是现在,我找不到正确重定向到此路由的方法。
当我在控制台中 运行 一个 rake routes
时,我只有
GET /:user_id/:id(.:format) projects#show
没有找到像我的旧 users_project_path
.
我错过了什么吗?谢谢!
你删除路径是因为这个原因你可以得到route_path,用as建立路由名。
resources :users, :path => '', only: [] do
get :projects, "/:user_id/:id", :to => "project#show", :as => :project_user
end
或者如果您不想使用,您可以使用此代码生成路径:
url_for([@user, @project])
我最近做了类似的事情,试试下面的方法
scope '/:user_id', as: :user do
resources :projects, path: '', only: [:show]
end
运行 rake routes
应该给你
user_project GET /:user_id/:id(.:format) projects#show
这将为您提供合适的 user_project_path(:user_id, :id)
路由助手。