【问题标题】:Rails nested resource - setting param: issueRails 嵌套资源 - 设置参数:问题
【发布时间】:2016-11-18 10:43:32
【问题描述】:

我声明了以下路线:

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

所以帐户、说明、消息是我拥有的 3 个模型。这给了我路线:

    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

这对我来说看起来不对,我在期待

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

有人可以告诉我我做错了什么吗?

【问题讨论】:

  • 你得到这个是因为你在嵌套结构中传递参数

标签: ruby-on-rails routes ruby-on-rails-5 nested-routes


【解决方案1】:

您可以通过以下方式进行:

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_id/instructions/:id我希望这是/accounts/:account_id/instructions/:instruction_id
猜你喜欢
  • 1970-01-01
  • 2011-05-25
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
相关资源
最近更新 更多