Peck3277
Peck3277

Reputation: 1423

How to create a route under a nested resource and namespace in rails?

I'm trying to create the below route but I am running into a "routing error" of "uninitialized constant Foo::Bar::Biz"

/foo/bar/biz/<biz_id>/custom

My routes file is as follows:

  namespace(:foo) do
    namespace(:bar) do
      resources(:biz, only: []) do
        get('/custom' => 'foo/bar/biz/custom#index') # I have also tried just custom#index
      end
    end
  end

When I run rake routes I see the route and the controller.

/foo/bar/biz/:biz_id/custom(.:format) foo/bar/biz/custom#index

My controllers file structure is this:

controllers/foo/bar/biz/custom_controller.rb

I don't currently have a controller for biz, but I have tested with one present.

My custom_controller is as follows:

module Foo
  module Bar
    module Biz
      class CustomController < FooController
        def index
          // do something
        end
      end
    end
  end
end

I suspect my routes are setup correctly and my error is in my controller or module setup. Is there something I am missing?

Upvotes: 0

Views: 326

Answers (1)

mechnicov
mechnicov

Reputation: 15288

Change your path

'foo/bar/biz/custom#index'

to path starting with root (/)

namespace(:foo) do
  namespace(:bar) do
    resources(:biz, only: []) do
      get('/custom' => '/foo/bar/biz/custom#index')
    end
  end
end

Upvotes: 0

Related Questions