是否可以命名整个 Rails 应用程序(即添加到 url 路径) [英] is it possible to namespace a whole rails app (ie adding to the url path)

查看:50
本文介绍了是否可以命名整个 Rails 应用程序(即添加到 url 路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 应用程序,在 routes.rb 中有大约 30 个匹配项

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

有没有办法在全球范围内应用它?然后为root设置一个例外?以及像 domain.com/user-name 这样漂亮的 url 的单一路由?

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?

推荐答案

使用 命名空间.

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

这篇关于是否可以命名整个 Rails 应用程序(即添加到 url 路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆