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'
# ...
delete ':folder_slug/:file_slug' => 'files#delete', :as => 'folder_file'
Something like this