开发者

How can I remove the resource type segment from my URI?

My project has files and folders, represented by File and Folder models. The project is functionally complete, and now I'm looking to cleanup the URI scheme while keeping my named routes.

My route contains a simple nested resource:

resources :folders do
  resources :files
end

Currently, folder_file_path(@folder, @file) outputs:

/folders/12/files/3

I've overridden to_param to output a human-readable slug, yielding

/folders/my-folder/files/my-file

I'd like to go a step further and remove the redundant folders and files segments from the URI. How can I modify my nested resources so that folder_file_path will output the following?

/my-folder/my-file

I'd like to keep my routes simple, using nested resources instead of moving to a longer mess of custom named routes, which is how I've currently achieved this:

get ':folder_slug' => 'folders#show', :as => 'folder'
get ':folder_slug/edit => 'folders#edit', :as => 'edit_folder'
# ...
delete ':folder_slug' => 'folders#destroy', : as => 'folder'

get ':folder_slug/:file_slug' => 'files#show', :as => 'folder_file'
get ':folder_slug/:file_slug/edit' => 'files#edit', :as => 'edit_folder_file'
# ...
delet开发者_如何学JAVAe ':folder_slug/:file_slug' => 'files#delete', :as => 'folder_file'


Something like this

resources :folders, path: ''
resources :folders, path: '', only: [] do
  resources :files, path: ''    
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜