Rails Tip: Prohibit certain route confuse other
Hi,
I searched the internet about the problem below, but it still fails.
In my rails application I had somes routes similar to the following:
SomeApp::Application.routes.draw do root :to => 'home#index' resources :person match '/:username' => 'users#show', :as => :profile end
And I had a problem with routes’ priority and I put a username route at my last line in routes.rb. But the another problem was found if I had Rails Engine routes? (Example: an Rails Engine to blog with routes as posts and comments)
Remembering: the Rails Engine routes load after Rails routes of an application.
And the Record Not Found Exception was raised (I was configured to rendered 404 page on this exception), because the username posts or comments was not found in the database. Solution?
SomeApp::Application.routes.draw do
root :to => 'home#index'
resources :person
match '/:username' => 'users#show', :as => :profile, :constraints => lambda { |request| User.find_by_username(request.path_parameters[:username]) != nil }
end
With the constraints added at my last line, my application avoid the username param was the same name of another route, the Record Not Found exception was raised passing to routes configured at Rails Engine.
I hope this tip can help others with similar problems.
Thanks,



