问题
I have a rails app that has approx 30 matches in routes.rb
match '/send-email' => 'index#send_email'
match '/forgot-password' => 'users#forgot_password', :as => :forgot_password
match '/forgot-password-confirmation' => 'users#forgot_password_confirmation'
match '/user/save-password' => 'users#save_password', :as => 'edit_users_password'
match '/user/home', :to => 'users#home'
match '/users/:id', :to => 'users#show', :as => "user_show"
What would be the best way to add a prepending route?
like /user/home -> /app/user/home,
/user/show/:id -> /app/user/show/:id
Is there a way to just apply this globally? And then put in an exception for root? and a single route for pretty urls like domain.com/user-name?
回答1:
Use a namespace.
namespace :app do
match '/send-email' => 'index#send_email'
match '/forgot-password' => 'users#forgot_password', :as => :forgot_password
match '/forgot-password-confirmation' => 'users#forgot_password_confirmation'
match '/user/save-password' => 'users#save_password', :as => 'edit_users_password'
match '/user/home', :to => 'users#home'
match '/users/:id', :to => 'users#show', :as => "user_show"
end
回答2:
I think the easiest way is to use scope in your routes It will work without name prefix as if you use namespace example
scope "/app" do
match '/send-email' => 'index#send_email'
match '/forgot-password' => 'users#forgot_password', :as => :forgot_password
match '/forgot-password-confirmation' => 'users#forgot_password_confirmation'
match '/user/save-password' => 'users#save_password', :as => 'edit_users_password'
match '/user/home', :to => 'users#home'
match '/users/:id', :to => 'users#show', :as => "user_show"
end
来源:https://stackoverflow.com/questions/9284615/is-it-possible-to-namespace-a-whole-rails-app-ie-adding-to-the-url-path