Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails嵌套资源-设置参数:问题_Ruby On Rails_Routes_Ruby On Rails 5_Nested Routes - Fatal编程技术网

Ruby on rails Rails嵌套资源-设置参数:问题

Ruby on rails Rails嵌套资源-设置参数:问题,ruby-on-rails,routes,ruby-on-rails-5,nested-routes,Ruby On Rails,Routes,Ruby On Rails 5,Nested Routes,我已申报下列路线: resources :accounts, param: :account_id do resources :instructions, param: :instruction_id do resources :messages, param: :message_id end end 因此,帐户、说明、消息是我拥有的三种模型。这给了我路线: account_instruction_messages GET /accou

我已申报下列路线:

  resources :accounts, param: :account_id do
     resources :instructions, param: :instruction_id  do
       resources :messages, param: :message_id
     end
  end
因此,帐户、说明、消息是我拥有的三种模型。这给了我路线:

    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
这在我看来是不对的,我早就料到了

/账户/:账户id/指令/:指令id等


有人能告诉我哪里做错了吗?

你可以用下一种方法来做:

resources :accounts do
 resources :instructions  do
   resources :messages, param: :message_id
 end
end
这将使您了解下一条路线:

    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
更新

但如果您需要所有相同的参数,则可以执行以下操作:

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
这将返回:

    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

问题是:
/accounts/:account\u id/instructions/:id
我想这应该是
/accounts/:account\u id/instructions/:instruction\u id
您之所以得到它,是因为您在嵌套结构中传递参数