两个资源之间无法解释的路由差异



在 Rails 4 中,对于用户和组织,我定义了相同的路由:

Rails.application.routes.draw do
  # Static pages
  root                        'static_pages#home'
  get 'hello'             =>  'static_pages#hello'
  get 'partner'           =>  'static_pages#partner'
  # Messages contact form
  get 'contact'           =>  'messages#new', as: 'contact'
  post 'contact'          =>  'messages#create'
  # Users
  get 'signup'            =>  'users#new'
  resources :users
  get 'admins'            =>  ‘users#index_adm'
  get 'unactivated'       =>  ‘users#index_unactivated'
  # Organizations
  resources :organizations
  # Sessions (for remembering log in log out)
  get    'login'          => 'sessions#new'
  post   'login'          => 'sessions#create'
  delete 'logout'         => 'sessions#destroy'
  # Account activation (sends email and uses edit method to change activation status)
  resources :account_activations, only: [:edit]
  # Password reset
  resources :password_resets,     only: [:new, :create, :edit, :update]
end

但是当我检查rake routes时,路径/路线不同:

      signup GET    /signup(.:format)                users#new
       users GET    /users(.:format)                 users#index
             POST   /users(.:format)                 users#create
    new_user GET    /users/new(.:format)             users#new
  edit_users GET    /users/:id/edit(.:format)        users#edit
        user GET    /users/:id(.:format)             users#show
             PATCH  /users/:id(.:format)             users#update
             PUT    /users/:id(.:format)             users#update
             DELETE /users/:id(.:format)             users#destroy
organizations GET   /organizations(.:format)         organizations#index
              POST  /organizations(.:format)         organizations#create
new_organization  GET /organizations/new(.:format)      organizations#new
edit_organization GET /organizations/:id/edit(.:format) organizations#edit
              GET    /organizations/:id(.:format)    organizations#show
              PATCH  /organizations/:id(.:format)    organizations#update
              PUT    /organizations/:id(.:format)    organizations#update
              DELETE /organizations/:id(.:format)    organizations#destroy

特别要注意用户的最后四行和组织的后四行。在这里,users#update PATCH请求与"用户路径"相关,而organizations#update PATCH请求与edit_organization路径相关。我希望组织路由与用户相同。我按照 Hartl railstutorial.org 设置的用户,并自己添加了组织。尝试更新组织的记录时,路由的差异会导致问题。

我做了哪些不同的操作,使组织路径与用户的路径不相似?

PATCH 和 PUT 操作与edit_organizationedit_users路径无关。当使用resources rails时,会为修改资源的操作生成路由,[PUT, PATCH, DELETE]资源的单数名称(organization_path)。

我相信您可能只是读取了错误的输出rake routes或者输出只是错误。除非rails抱怨organization_path不存在。

问题消失了。这可能是我正在使用的云程序的问题。重新启动浏览器后,问题突然消失了...

最新更新