You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在多子域名场景下从地址栏移除Devise资源名称

Solution for Clean Subdomain Routes in Rails

Got it, let's fix this routing issue so your subdomain-based paths are clean and don't have those redundant resource prefixes. Here's how to adjust your routes.rb to make that happen:

Modified routes.rb Code

# School subdomain routes
constraints subdomain: 'school' do
  # School admin Devise routes (no /schools prefix)
  devise_for :schools, 
             skip: [:registrations], 
             path: '',
             controllers: { sessions: 'schools/sessions', passwords: 'schools/passwords' }
  
  devise_scope :school do
    get 'edit' => 'schools/registrations#edit', as: 'edit_school_registration'
    put '' => 'schools/registrations#update', as: 'school_registration'
  end

  resources :school, path: '' # Removes /school prefix for resource routes
end

# Employee subdomain routes
constraints subdomain: 'employee' do
  # Employee Devise routes (no /employees prefix)
  devise_for :employees, 
             skip: [:registrations], 
             path: '',
             controllers: { sessions: 'employees/sessions', passwords: 'employees/passwords' }
  
  devise_scope :employee do
    get 'edit' => 'employees/registrations#edit', as: 'edit_employee_registration'
    put '' => 'employees/registrations#update', as: 'employee_registration'
  end

  resources :employee, path: '' # Removes /employee prefix for resource routes
end

# Student subdomain routes
constraints subdomain: 'student' do
  # Student Devise routes (no /students prefix)
  devise_for :students, 
             skip: [:registrations], 
             path: '',
             controllers: { sessions: 'students/sessions', passwords: 'students/passwords' }
  
  # Fixed the devise_scope target (was incorrectly set to :school_district)
  devise_scope :student do
    get 'edit' => 'students/registrations#edit', as: 'edit_student_registration'
    put '' => 'students/registrations#update', as: 'student_registration'
  end

  resources :student, path: '' # Removes /student prefix for resource routes
end

Key Changes Explained

  • Subdomain Constraints: Wrapped each resource's routes in constraints subdomain: 'xxx' so Rails knows exactly which routes to trigger based on the incoming subdomain.
  • Remove Devise Path Prefix: Added path: '' to devise_for calls—this eliminates the default /schools, /employees, /students prefixes from Devise routes, so you get /sign_in instead of /schools/sign_in.
  • Adjust Devise Scope Routes: Updated the get and put paths in devise_scope to remove resource prefixes (e.g., get 'edit' instead of get 'schools/edit').
  • Fix Typo: Corrected the devise_scope target for students from :school_district to :student—this was a bug that would have broken registration edit/update routes.
  • Clean Resource Routes: Added path: '' to resources calls to remove the resource prefix for standard CRUD routes (if you need them).

Important Setup Notes

  1. Configure TLD Length: In your environment config file (e.g., config/environments/production.rb), set config.action_dispatch.tld_length = 2 (adjust if you're using a sub-TLD like .co.uk). This helps Rails correctly identify subdomains.
  2. Local Testing: To test locally, add entries to your hosts file:
    127.0.0.1 school.mydomain.com
    127.0.0.1 employee.mydomain.com
    127.0.0.1 student.mydomain.com
    

内容的提问来源于stack exchange,提问作者Rohit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:52:41