My application currently has a very simple URL layout:
map.resource :account, :controller => "users", :only => [:show, :update, :destroy]
map.resources :workouts do |workout|
workout.resource :chart, :only => [:show]
workout.resources :taggings, :only => [:destroy, :create]
end
Giving me nice easy urls for the current user.
I’m now adding some public features – the ability for the public to see items. I’d like to prepend a user’s login name to these URLs. At the same time, there’s no need for public users to have access to anything besides the one workout resource. I’m not sure the best way to do this. I’d like to wind up with URLs that look like:
/teich/workouts/3454
Which is roughly
/:user_login/workouts/:id
Do I path_prepend? For the current user, who wants the full RESTful controls, is it best practice to still have their username prepend instead of direct URLs as I have now? Do I not worry abut all the extra routes with the path_prepend since I’ll be :before permission checking anyway?
Probably the easiest way to acheive something like this is to override
to_paramin yourUsermodel.Rails uses this method when generating paths for the model. Then, when doing finds the string sends
to_iwhich will grab the integer.Don’t forget you’ll need to escape non-valid url characters too.