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

时间:2015-04-18 14:09:01

标签: ruby-on-rails ruby-on-rails-4 routes

在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设置并自己添加了组织。尝试更新组织的记录时,路线的差异会导致问题。

我做了哪些不同的事情,组织路径与用户的路径不相似?

2 个答案:

答案 0 :(得分:0)

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

我相信你可能只是在读rake routes错误的输出 - 或输出是错误的。这是因为rails抱怨organization_path不存在。

答案 1 :(得分:0)

问题已经消失。这可能是我工作的云计划的一个问题。突然重启我的浏览器后,问题就消失了......