2

I'm trying to figure out how to do some routing in a Rails 4 app. This is my first question, so I'll do my best.

I have accounts. A user can belong to multiple accounts. An account can have multiple "modules" enabled -- buyer / seller.

resources :accounts do
  member do
    namespace :seller do
      resources :contracts do
        get "history", on: :member
      end
    end
    namespace :buyer do
      resources :contracts do
        get "history", on: :member
      end
    end
  end
end

Ideally, the accounts, and contracts (in each namespace) would remain resourceful.

The problem is that when you see the routes that are created, the nested contract resource, and the account are both using :id in the params

stuff_buyer_contract GET /accounts/:id/buyer/contracts/:id/stuff(.:format)

I assume I'll get a bunch of "shallow routes" answers, but is there anyway to do it as is, where the :id params will be correct?

I would like the routing to look like:

stuff_buyer_contract GET /accounts/:account_id/buyer/contracts/:id/stuff(.:format)

2
  • HOw do you like the routes to be ? /accounts/buyer/contracts/:id/stuff Commented Dec 12, 2014 at 18:52
  • @Saravanan I modified the question. See if that helps. The issue is that once it goes into a namespace, it seems to lose its context, and no longer knows about the nested resource it seems. Commented Dec 12, 2014 at 19:31

1 Answer 1

1

Try the below route configuration:

  resources :accounts do
      namespace :seller do
        resources :contracts do
          get "history", on: :member
        end
      end
      namespace :buyer do
        resources :contracts do
          get "history", on: :member
        end
      end
  end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! The addition of the "member do" was what caused the issue.

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.