如何在多子域名场景下从地址栏移除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: ''todevise_forcalls—this eliminates the default/schools,/employees,/studentsprefixes from Devise routes, so you get/sign_ininstead of/schools/sign_in. - Adjust Devise Scope Routes: Updated the
getandputpaths indevise_scopeto remove resource prefixes (e.g.,get 'edit'instead ofget 'schools/edit'). - Fix Typo: Corrected the
devise_scopetarget for students from:school_districtto:student—this was a bug that would have broken registration edit/update routes. - Clean Resource Routes: Added
path: ''toresourcescalls to remove the resource prefix for standard CRUD routes (if you need them).
Important Setup Notes
- Configure TLD Length: In your environment config file (e.g.,
config/environments/production.rb), setconfig.action_dispatch.tld_length = 2(adjust if you're using a sub-TLD like.co.uk). This helps Rails correctly identify subdomains. - Local Testing: To test locally, add entries to your
hostsfile:127.0.0.1 school.mydomain.com 127.0.0.1 employee.mydomain.com 127.0.0.1 student.mydomain.com
内容的提问来源于stack exchange,提问作者Rohit
相关产品推荐
相关产品推荐

