两种资源之间无法解释的路由差异
Unexplainable difference in routes between two resources
在Rails4中,我为用户和组织定义了相同的路由:
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
时,paths/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
请求与 'user path' 相关,而 organizations#update PATCH
请求与 edit_organization
路径相关。我希望组织路线与用户相同。我根据 Hartl 的 railstutorial.org 设置了用户,并自己添加了组织。尝试更新组织的记录时,路由差异会导致问题。
组织路径与用户路径不相似,我做了什么不同的事情?
PATCH 和 PUT 操作与 edit_organization
或 edit_users
路径无关。使用 resources
时,rails 会为修改资源 [PUT, PATCH, DELETE]
的操作生成路由,使其脱离资源的单数名称 ( organization_path
)。
我相信您可能只是读错了 rake routes
的输出 - 或者输出有问题。除非 rails 抱怨 organization_path
不存在。
问题解决了。这可能是我工作的云程序的问题。突然重启我的浏览器后问题就消失了...
在Rails4中,我为用户和组织定义了相同的路由:
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
时,paths/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
请求与 'user path' 相关,而 organizations#update PATCH
请求与 edit_organization
路径相关。我希望组织路线与用户相同。我根据 Hartl 的 railstutorial.org 设置了用户,并自己添加了组织。尝试更新组织的记录时,路由差异会导致问题。
组织路径与用户路径不相似,我做了什么不同的事情?
PATCH 和 PUT 操作与 edit_organization
或 edit_users
路径无关。使用 resources
时,rails 会为修改资源 [PUT, PATCH, DELETE]
的操作生成路由,使其脱离资源的单数名称 ( organization_path
)。
我相信您可能只是读错了 rake routes
的输出 - 或者输出有问题。除非 rails 抱怨 organization_path
不存在。
问题解决了。这可能是我工作的云程序的问题。突然重启我的浏览器后问题就消失了...