Rails控制器使用名称空间覆盖路由

时间:2019-05-13 10:39:15

标签: ruby-on-rails ruby routes overloading

我有一个Rails应用,其中的路由使用名称空间定义;

routes.rb:

resources :customers do
  resources :employees do
    # [1] First absence controller
    resources :absences, only: %i{ index new create } do
      # omit verbose
    end
  end
end

scope module: :employee, path: :employee, as: :employee do
  # [2] Second absence controller
  resources :absences, only: %i{ index new create } do
    # omit verbose
  end
end

rails routes -c absences中提取:

                            Prefix Verb URI Pattern                                                                    Controller#Action
reported_customer_employee_absence POST /customers/:customer_id/employees/:employee_id/absences/:id/reported(.:format) absences#reported
        customer_employee_absences GET  /customers/:customer_id/employees/:employee_id/absences(.:format)              absences#index
                                   POST /customers/:customer_id/employees/:employee_id/absences(.:format)              absences#create
     new_customer_employee_absence GET  /customers/:customer_id/employees/:employee_id/absences/new(.:format)          absences#new
            avoid_employee_absence POST /employee/absences/:id/avoid(.:format)                                         employee/absences#avoid
                 employee_absences GET  /employee/absences(.:format)                                                   employee/absences#index
                                   POST /employee/absences(.:format)                                                   employee/absences#create
              new_employee_absence GET  /employee/absences/new(.:format)                                               employee/absences#new

所有操作均在开发和测试环境中按预期运行。

但是在生产环境中,问题在于,当它调用“员工/缺勤” URL时,Rails运行的是第一个[1]而不是第二个[2]控制器。

我的版本:

 Env  | Ruby      | Rails   | Gem
--------------------------------------
 Dev  | 2.5.1p57  | 5.2.3   | 2.7.6
 Test | 2.5.6p159 | 5.2.2.1 | 2.7.6.2
 Prod | 2.3.3p222 | 5.2.3   | 2.5.2.1

-稍后添加-

也许这是一个声明问题:

absences_controller.rb

class AbsencesController < ApplicationController
end

employee / absences_controller.rb

class Employee::AbsencesController < Employee::ApplicationController
end

employee/absence_controller.rb是员工命名空间中的第一个控制器,它有问题或类似问题吗?

0 个答案:

没有答案