嵌套的路线文件Rails

时间:2019-07-15 19:04:29

标签: ruby-on-rails ruby routes ruby-on-rails-5.2

我在config / initializers中使用以下内容制作了一些嵌套的路由文件:

class ActionDispatch::Routing::Mapper
  def draw(routes_name, sub_path=nil)
    if sub_path.present?
      instance_eval(File.read(Rails.root.join("config/routes/#{sub_path}/#{routes_name}.rb")))
    else
      instance_eval(File.read(Rails.root.join("config/routes/#{routes_name}.rb")))
    end
  end
end

但是,当在路由文件中这样使用时:

Rails.application.routes.draw do
  scope :api do
    ["v1"].map { |version| draw :base, "api/" + version }
  end
end

那里的路由似乎没有嵌套为api/users等。不确定为什么这里的作用域被忽略了。

编辑:更详细的示例

routes.rb

namespace :api, defaults: { format: :json } do
    Rails.application.routes.draw do
      ["v1"].map { |version| draw :base, "api/" + version }
    end
end

base.rb

  namespace :v1 do
      Rails.application.routes.draw do
        [:identity].map { |path| draw path, "api/v1"}
      end
  end

identity.rb

Rails.application.routes.draw do
      # Ensures proper namespace when fetching controllers, but does not add to path for routes
      scope module: :identity do

        namespace :users do
          put '/', action: :update
          patch '/', action: :update
          get 'user_from_token', action: :update
        end

      end
end

1 个答案:

答案 0 :(得分:1)

Rails.application.routes.draw是多余的

# config/routes.rb
Rails.application.routes.draw do
  scope :api do
    ["v1"].map { |version| draw :base, "api/" + version }
  end
end
# config/routes/api/v1/base.rb
namespace :v1 do
  # Rails.application.routes.draw do
    [:identity].map { |path| draw path, "api/v1"}
  # end
end
# config/routes/api/v1/identity.rb
# Rails.application.routes.draw do
  # Ensures proper namespace when fetching controllers, but does not add to path for routes
  scope module: :identity do

    namespace :users do
      put '/', action: :update
      patch '/', action: :update
      get 'user_from_token', action: :update
    end

  end
# end
$ rake routes
                   Prefix Verb  URI Pattern                                                                              Controller#Action
                 v1_users PUT   /api/v1/users(.:format)                                                                  v1/identity/users#update
                          PATCH /api/v1/users(.:format)                                                                  v1/identity/users#update
 v1_users_user_from_token GET   /api/v1/users/user_from_token(.:format)                                                  v1/identity/users#update
       rails_service_blob GET   /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
rails_blob_representation GET   /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
       rails_disk_service GET   /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
update_rails_disk_service PUT   /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
     rails_direct_uploads POST  /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create