I'm trying to create a nested resource with a url scheme along the lines of: "http://example.com/username/...
".
What I currently have is this:
ActionController::Routing::Routes.draw do |map|
map.home '/', :controller => 'home'
map.resource :session
map.resources :users, :has_many => :nodes
#map.user '/:id', :controller => 'users', :action => 'show', :has_many => :nodes
map.resources :nodes, :belongs_to => :user
end
This results in URLs like:
http://example.local/users/username
http://example.local/users/username/nodes
How to avoid the "users" prefix is beyond me. Passing a "as: => ''
" option to map.resources
doesn't work and it seems named routes don't support the ":has_many
" or ":belongs_to
" options.
Commenting out the "map.resources :users
" and uncommmenting the "map.user
" line after it seems to work… until you reach a nested resource. Then it spits out the following error:
undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>
I know this problem has come up plenty of times before and is always met with "Why would you want to do that?" responses. Frankly, Twitter does it, Facebook does it, and I want to do it too! ;-D
As for the common criticism of how to avoid usernames from conflicting built-in paths, I've set my minimum username length to 6 characters and plan to make all built-in root-level paths segments paths 5 characters or shorter (i.e. "/opt/...
" for options, "/in/...
" for session log-in, etc.).
Thanks a ton in advance and hopefully there's a solution out there that everyone can use!