5

I currently have this in my routes.rb :

namespace :api
  namespace :v1
   namespace :me
    # ...
    resources :offers do
     resources :state, only: %i(index)
    end
  end
 end
end

This gives me this route :

GET v1/me/offers/:offer_id/state(.:format)
api/v1/me/state#index

But the route I would like to have is this one :

GET v1/me/offers/:offer_id/state(.:format)
api/v1/me/offers/state#index

Simply put, I want to be able to place my state_controller.rb in an offers folder, without changing the path to access it. How can I achieve that ?

3 Answers 3

10

I found a better way to do it : use module

resources :offers, module: :offers do
  resources :state, only: %i(index)
end
Sign up to request clarification or add additional context in comments.

Comments

2

You should define controller for your resources explicitly:

resources :state, controller: 'offers/state'

This will route requests to app/controllers/api/v1/me/offers/state_controller.rb.

3 Comments

Is there no way to achieve this without specifying the controller ? Looks a bit "ugly" to me :(
I'm not sure but it might work as well: controller: 'offers/state'. I cannot think about anything else.
Indeed, it works with controller: 'offers/state'. However, it doesn't with controller: 'api/v1/me/offers/state' (the path is not right). You should edit your answer :)
2
namespace :api
  namespace :v1
   namespace :me
    # ...
    resources :offers do
      namespace :offers, path: "" do
        resources :state, only: %i(index)
      end
    end
  end
 end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.