RoutingError(没有路由匹配[PUT]

时间:2014-02-21 10:03:33

标签: ruby-on-rails-3 devise routes

我对我的路线有疑问。我使用设计,但我也有自定义控制器,以允许管理员编辑用户设置。

在我的情况下,一切都在开发中工作正常但是当我部署到我的托管服务器时,事情根本不能正常工作。

我遇到的问题如下,当管理员更新用户时,我收到路由错误。这在本地工作正常,但在托管服务器上我得到以下

Started PUT "/users/4"  at Fri Feb 21 04:08:14 -0500 2014
ActionController::RoutingError (No route matches [PUT] "/4"):
  actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'>  actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'

对我来说不正确的是“/ users / 4”如何被剥离到“/ 4”。其余的路径怎么了?

我的路线看起来很好

users POST   /users(.:format) users/registrations#create
      new_user_session GET    /users/sign_in(.:format)                  devise/sessions#new
            user_session POST   /users/sign_in(.:format)                  devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                 devise/sessions#destroy
           user_password POST   /users/password(.:format)                 devise/passwords#create
       new_user_password GET    /users/password/new(.:format)             devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)            devise/passwords#edit
                         PUT    /users/password(.:format)                 devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                   users/registrations#cancel
       user_registration POST   /users(.:format)                          users/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                  users/registrations#new
  edit_user_registration GET    /users/edit(.:format)                     users/registrations#edit
                         PUT    /users(.:format)                          users/registrations#update
                         DELETE /users(.:format)                          users/registrations#destroy
        new_user_session GET    /users/sign_in(.:format)                  devise/sessions#new
                         POST   /users/sign_in(.:format)                  devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                 devise/sessions#destroy
                         POST   /users/password(.:format)                 devise/passwords#create
                         GET    /users/password/new(.:format)             devise/passwords#new
                         GET    /users/password/edit(.:format)            devise/passwords#edit
                         PUT    /users/password(.:format)                 devise/passwords#update
                         GET    /users/cancel(.:format)                   devise/registrations#cancel
                         POST   /users(.:format)                          devise/registrations#create
                         GET    /users/sign_up(.:format)                  devise/registrations#new
                         GET    /users/edit(.:format)                     devise/registrations#edit
                         PUT    /users(.:format)                          devise/registrations#update
                         DELETE /users(.:format)                          devise/registrations#destroy
                         GET    /users(.:format)                          users#index
                         POST   /users(.:format)                          users#create
                new_user GET    /users/new(.:format)                      users#new
               edit_user GET    /users/:id/edit(.:format)                 users#edit
                    user GET    /users/:id(.:format)                      users#show
                         PUT    /users/:id(.:format)                      users#update
                         DELETE /users/:id(.:format)                      users#destroy

如您所见,用户更新路径存在

  

PUT /users/:id(.:format)用户#update

主机服务器正在使用passenger,rails 3.2.X,ruby 1.8.7。我知道3.2.x版本存在安全问题,但这只是我的一个爱好。

路由配置

resources :comm_channels

devise_for :users, :controllers => { :registrations => "users/registrations" } do
  post '/users', :to => 'users/registrations#create'
end

devise_for :users

resources :users,:roles,:news ,:invites, :clubs, :events, :albums, :pictures, :static_pages, :sponsors

match "static_pages/:action/:pagetype", :controller => "static_pages"
match 'invites/edit(/:eid/:iid/:code)' => 'invites#edit', :as => :invite_confirm
match 'events/:id/reminder' => 'events#reminder', :as => :event_reminder
match 'events/:id/republish' => 'events#republish', :as => :event_republish
match 'roles/resetMembership' => 'roles#resetMembership', :as => :reset_membership
match 'roles/membership_reminder' => 'roles#membership_reminder', :as => :membership_reminder

root :to => "static_pages#show#home"

更新2

我已经把Tim的评论记录在船上并清理了我的路线文件但是我仍然看到同样的问题。

我的路线现在看起来像

resources :comm_channels

devise_for :users,:path => 'club_users', :controllers => { :registrations => "users/registrations" } do
  post '/users', :to => 'users/registrations#create'
end


resources :users

resources :roles,:news ,:invites, :clubs, :events, :albums, :pictures, :static_pages, :sponsors

match "static_pages/:action/:pagetype", :controller => "static_pages"
match 'invites/edit(/:eid/:iid/:code)' => 'invites#edit', :as => :invite_confirm
match 'events/:id/reminder' => 'events#reminder', :as => :event_reminder
match 'events/:id/republish' => 'events#republish', :as => :event_republish
match 'roles/resetMembership' => 'roles#resetMembership', :as => :reset_membership
match 'roles/membership_reminder' => 'roles#membership_reminder', :as => :membership_reminder

root :to => "static_pages#show#home"

路由输出如下

        users POST   /users(.:format)                          users/registrations#create
        new_user_session GET    /club_users/sign_in(.:format)             devise/sessions#new
            user_session POST   /club_users/sign_in(.:format)             devise/sessions#create
    destroy_user_session DELETE /club_users/sign_out(.:format)            devise/sessions#destroy
           user_password POST   /club_users/password(.:format)            devise/passwords#create
       new_user_password GET    /club_users/password/new(.:format)        devise/passwords#new
      edit_user_password GET    /club_users/password/edit(.:format)       devise/passwords#edit
                         PUT    /club_users/password(.:format)            devise/passwords#update
cancel_user_registration GET    /club_users/cancel(.:format)              users/registrations#cancel
       user_registration POST   /club_users(.:format)                     users/registrations#create
   new_user_registration GET    /club_users/sign_up(.:format)             users/registrations#new
  edit_user_registration GET    /club_users/edit(.:format)                users/registrations#edit
                         PUT    /club_users(.:format)                     users/registrations#update
                         DELETE /club_users(.:format)                     users/registrations#destroy
                         GET    /users(.:format)                          users#index
                         POST   /users(.:format)                          users#create
                new_user GET    /users/new(.:format)                      users#new
               edit_user GET    /users/:id/edit(.:format)                 users#edit
                    user GET    /users/:id(.:format)                      users#show
                         PUT    /users/:id(.:format)                      users#update
                         DELETE /users/:id(.:format)                      users#destroy

我仍然在生产设置中看到相同的错误。

Started PUT "/users/21" for 194.XXXX at Sun May 11 08:05:48 -0400 2014

ActionController::RoutingError (No route matches [PUT] "/21"):
  actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/static.rb:61:in `call'

我尝试了不同的最简单的变化,例如

devise_for :users,:path => 'club_users'
resources :users

还有相反的

devise_for :users
resources :users, :path => 'useradmin'

没有快乐。正如我之前所说的,rake路线告诉我那里

PUT    /users/:id(.:format)                      users#update

但在某个地方,PUT“/ users / 21”被剥离为“/ 21”。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我不确定为什么只有那条特定的路线会给你带来麻烦而且只是在生产中,但我认为你的路线存在一些普遍的问题,如果你修复了,可能会解决这个特定路线的问题。

首先,你有:

devise_for :users, :controllers => { :registrations => "users/registrations" } do
  post '/users', :to => 'users/registrations#create'
end

devise_for :users

正如您从路线中看到的那样,devise_for的重复导致您的大多数路线重复,除了每条路线有两份副本的登记路线;其中一半转到你被覆盖的注册控制器(因为第一个devise_for),其中一半去设计注册控制器(因为第二个devise_for。你应该只有一个devise_for你保留哪一个取决于你想要注册的人使用哪个注册控制器。假设你想让他们使用你被覆盖的版本,你应该删除第二个devise_for。另外,在第一个{{1我认为你可以删除devise_for行,因为这是你无论如何都会得到的。

完成此操作后,您将拥有一组设计路线以及其他用户路线(来自post)。但是,你仍然有问题;你还有:

resources :users

来自user_registration POST /users(.:format) users/registrations#create ,并且:

devise_for

因为 POST /users(.:format) users#create 。相同的http方法和路径映射到不同的控制器,这是行不通的。我建议你给用户控制器一个不同的路径,以便在注册和登录等工作(设计)和你的用户管理控制器之间保持清晰的分离。而不是:

resources :users

试试这个:

resources :users,:roles,:news ,:invites, :clubs, :events, :albums, :pictures, :static_pages, :sponsors

或您想要的任何合适的路径名,而不是resources :users, :path => 'useradmin' resources :roles, :news, :invites, :clubs, :events, :albums, :pictures, :static_pages, :sponsors 。这只会改变路径;它会保持路由助手和预期的控制器名称相同,所以你应该有:

useradmin

或者你可以做相反的事情;将您的用户控制器保留在/ users,并使用 new_user GET /useradmin/new(.:format) users#new edit_user GET /useradmin/:id/edit(.:format) users#edit user GET /useradmin/:id(.:format) users#show PUT /useradmin/:id(.:format) users#update DELETE /useradmin/:id(.:format) users#destroy 作为:path => 'whatever-you-want'的参数将设计控制器映射到另一个路径。

一旦你有了这样一个干净的师,希望你的错误会消失!

<强>更新

我发现路径中仍有一些重叠(devise_for输出中的初始users POST /users(.:format) users/registrations#create,虽然这是POST而不是PUT,所以似乎就像它一样应该是PUT的问题(除非您在不同的上下文中使用关联的rake routes帮助程序。)而是与后面映射到users_path冲突。这种重叠是由于{ {1}}中的{1}}。但是,你有这个额外的映射吗?它等同于POST /users(.:format) users#create,而只是post '/users', :to => 'users/registrations#create'而不是devise_for。你能不能删除它所以没有路径重叠?我担心它有效果,虽然我猜测有点没有控制器/视图代码。

顺便说一句,我也在开发中使用webrick。虽然我使用的是Ruby 1.9.3,但是产品和Rails 3.2.17,设计等等。我没有看到任何明显的原因导致他们自己成为问题的原因。它更可能是路由设置。