1

I have the following routes declared:

  resources :accounts, param: :account_id do
     resources :instructions, param: :instruction_id  do
       resources :messages, param: :message_id
     end
  end

So accounts, instructions, messages are the 3 models I have. This gives me the routes:

    account_instruction_messages GET    /accounts/:account_account_id/instructions/:instruction_instruction_id/messages(.:format)                  messages#index
                                 POST   /accounts/:account_account_id/instructions/:instruction_instruction_id/messages(.:format)                  messages#create
 new_account_instruction_message GET    /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/new(.:format)              messages#new
edit_account_instruction_message GET    /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id/edit(.:format) messages#edit
     account_instruction_message GET    /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format)      messages#show
                                 PATCH  /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format)      messages#update
                                 PUT    /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format)      messages#update
                                 DELETE /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format)      messages#destroy
            account_instructions GET    /accounts/:account_account_id/instructions(.:format)                                                       instructions#index
                                 POST   /accounts/:account_account_id/instructions(.:format)                                                       instructions#create
         new_account_instruction GET    /accounts/:account_account_id/instructions/new(.:format)                                                   instructions#new
        edit_account_instruction GET    /accounts/:account_account_id/instructions/:instruction_id/edit(.:format)                                  instructions#edit
             account_instruction GET    /accounts/:account_account_id/instructions/:instruction_id(.:format)                                       instructions#show
                                 PATCH  /accounts/:account_account_id/instructions/:instruction_id(.:format)                                       instructions#update
                                 PUT    /accounts/:account_account_id/instructions/:instruction_id(.:format)                                       instructions#update
                                 DELETE /accounts/:account_account_id/instructions/:instruction_id(.:format)                                       instructions#destroy

That looks wrong to me, I was expecting

/accounts/:account_id/instructions/:instruction_id etc...?

Can someone advise what I am doing wrong?

Haroon
  • 3,402
  • 6
  • 43
  • 74

1 Answers1

3

you can do it in next way:

resources :accounts do
 resources :instructions  do
   resources :messages, param: :message_id
 end
end

this will make you next routes:

    account_instruction_messages GET       /accounts/:account_id/instructions/:instruction_id/messages(.:format)                  messages#index
                                 POST      /accounts/:account_id/instructions/:instruction_id/messages(.:format)                  messages#create
 new_account_instruction_message GET       /accounts/:account_id/instructions/:instruction_id/messages/new(.:format)              messages#new
edit_account_instruction_message GET       /accounts/:account_id/instructions/:instruction_id/messages/:message_id/edit(.:format) messages#edit
     account_instruction_message GET       /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#show
                                 PATCH     /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#update
                                 PUT       /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#update
                                 DELETE    /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#destroy
            account_instructions GET       /accounts/:account_id/instructions(.:format)                                           instructions#index
                                 POST      /accounts/:account_id/instructions(.:format)                                           instructions#create
         new_account_instruction GET       /accounts/:account_id/instructions/new(.:format)                                       instructions#new
        edit_account_instruction GET       /accounts/:account_id/instructions/:id/edit(.:format)                                  instructions#edit
             account_instruction GET       /accounts/:account_id/instructions/:id(.:format)                                       instructions#show
                                 PATCH     /accounts/:account_id/instructions/:id(.:format)                                       instructions#update
                                 PUT       /accounts/:account_id/instructions/:id(.:format)                                       instructions#update
                                 DELETE    /accounts/:account_id/instructions/:id(.:format)                                       instructions#destroy
                        accounts GET       /accounts(.:format)                                                                    accounts#index
                                 POST      /accounts(.:format)                                                                    accounts#create
                     new_account GET       /accounts/new(.:format)                                                                accounts#new
                    edit_account GET       /accounts/:id/edit(.:format)                                                           accounts#edit
                         account GET       /accounts/:id(.:format)                                                                accounts#show
                                 PATCH     /accounts/:id(.:format)                                                                accounts#update
                                 PUT       /accounts/:id(.:format)                                                                accounts#update
                                 DELETE    /accounts/:id(.:format)                                                                accounts#destroy

UPDATE

but if you need all same params, you can do:

resources :accounts, only: [] do
 resources :instructions, only: []  do
   resources :messages, param: :message_id
 end
end
resources :accounts, param: :account_id
resources :instructions, param: :instruction_id

and this will return:

    account_instruction_messages GET       /accounts/:account_id/instructions/:instruction_id/messages(.:format)                  messages#index
                                 POST      /accounts/:account_id/instructions/:instruction_id/messages(.:format)                  messages#create
 new_account_instruction_message GET       /accounts/:account_id/instructions/:instruction_id/messages/new(.:format)              messages#new
edit_account_instruction_message GET       /accounts/:account_id/instructions/:instruction_id/messages/:message_id/edit(.:format) messages#edit
     account_instruction_message GET       /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#show
                                 PATCH     /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#update
                                 PUT       /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#update
                                 DELETE    /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format)      messages#destroy
                        accounts GET       /accounts(.:format)                                                                    accounts#index
                                 POST      /accounts(.:format)                                                                    accounts#create
                     new_account GET       /accounts/new(.:format)                                                                accounts#new
                    edit_account GET       /accounts/:accounts_id/edit(.:format)                                                  accounts#edit
                         account GET       /accounts/:accounts_id(.:format)                                                       accounts#show
                                 PATCH     /accounts/:accounts_id(.:format)                                                       accounts#update
                                 PUT       /accounts/:accounts_id(.:format)                                                       accounts#update
                                 DELETE    /accounts/:accounts_id(.:format)                                                       accounts#destroy
....... and so on
Oleh Sobchuk
  • 3,612
  • 2
  • 25
  • 41
  • The problem with this is: `/accounts/:account_id/instructions/:id` I would this to be `/accounts/:account_id/instructions/:instruction_id` – Haroon Nov 18 '16 at 11:32