is it possible to namespace a whole rails app (ie adding to the url path)

柔情痞子 提交于 2019-12-13 01:34:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!