Ruby on Rails:子域太强大了?我该如何正确设置

发布于 2024-10-04 14:42:35 字数 616 浏览 2 评论 0原文

路线:

  match '/' => 'site_admin/admin#index'


  resources :link_pages
  resources :services  
  resource :user_session
  resource :account, :controller => "users"
  resources :password_resets
  resources :users
  resources :addresses
  resources :info

  match "/home", :to => 'info#home'
  match "/register", :to => 'users#new'

  root :to => 'info#home'


  match ':controller(/:action(/:id(.:format)))'

所以当我到达 admin.lvh.me:3000/ 时,它会转到 site_admin/admin#index...这很棒... 但是当我取消子域时,只有 lvh.me:3000/ 它会走相同的路线....

我如何让管理员保持在原来的位置。没有子域可以转到我的根页面,就像我的路由文件中那样?

routes:

  match '/' => 'site_admin/admin#index'


  resources :link_pages
  resources :services  
  resource :user_session
  resource :account, :controller => "users"
  resources :password_resets
  resources :users
  resources :addresses
  resources :info

  match "/home", :to => 'info#home'
  match "/register", :to => 'users#new'

  root :to => 'info#home'


  match ':controller(/:action(/:id(.:format)))'

so when I got to admin.lvh.me:3000/ it goes to site_admin/admin#index... which is great...
but when I take off the subdomain, and just have lvh.me:3000/ it goes to the same route....

how do I get admin to stay where it is. and no subdomain to go to my root page, as in my routes file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里寻她 2024-10-11 14:42:35

路由按顺序解析,因此当您从任何域请求 / 时,它会首先找到“匹配'/'...”并将您发送到指定页面。您的子域根本不起作用。您可以使用基于请求的约束来基于子域进行路由:

http://guides。 rubyonrails.org/routing.html#request-based-constraints

Routes are parsed in order, so when you request / from any domain it finds "match '/'..." first and sends you to the specified page. Your subdomain isn't coming into play at all. You can use Request-based constraints to route based on subdomain:

http://guides.rubyonrails.org/routing.html#request-based-constraints

故人爱我别走 2024-10-11 14:42:35

根本不知道子域是如何影响这个的。也许您将子域与路由命名空间混淆了(http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing)?

match '/' => 'site_admin/admin#index'

被选择超过

root :to => 'info#home'

因为它首先在路由文件中定义。它们表面上是同一件事。

Not sure how subdomain factors into this at all. Perhaps you're confusing subdomain with route namespacing (http://edgeguides.rubyonrails.org/routing.html#controller-namespaces-and-routing)?

match '/' => 'site_admin/admin#index'

Is being selected over

root :to => 'info#home'

Because it's defined first in the routes file. They're ostensibly the same thing.

梦晓ヶ微光ヅ倾城 2024-10-11 14:42:35

是的@Cory 是对的。上面两个语句是相似的,并且每次都会考虑第一个定义的路线。如果您将管理路由更改为

match '/admin' => 'site_admin/admin#index'

then it does make sense... What say??

或者,使用以下代码,您可以有条件地确定您的 URL:

request.subdomains(0).first

will give you the subdomain value- either admin or blank. But it will go to any one controller action only which is defined first in route.rb file.

然后,从使用子域的操作中,您可以决定将其重定向到何处 - 管理面板或主页...

Yes @Cory is right. Above both statements are similar and first defined route is considered every time. If you change admin route to

match '/admin' => 'site_admin/admin#index'

then it does make sense... What say??

or else, using the following code you can determine your URL conditionally:

request.subdomains(0).first

will give you the subdomain value- either admin or blank. But it will go to any one controller action only which is defined first in route.rb file.

Then from that action using subdomain, you can decide where to re-direct it- either to admin panel or home page...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文