Rails RoutingError:没有路由匹配{:controller =>“sessions”,:action =>“destroy”}

时间:2011-04-14 21:02:10

标签: ruby-on-rails-3 routing

然而,当运行rake:路线似乎在那里:

     users GET    /users(.:format)          {:action=>"index", :controller=>"users"}
            POST   /users(.:format)          {:action=>"create", :controller=>"users"}
   new_user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
  edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
       user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
            PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
            DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}
   sessions POST   /sessions(.:format)       {:action=>"create", :controller=>"sessions"}
new_session GET    /sessions/new(.:format)   {:action=>"new", :controller=>"sessions"}
    session DELETE /sessions/:id(.:format)   {:action=>"destroy", :controller=>"sessions"}
     signup        /signup(.:format)         {:controller=>"users", :action=>"new"}
    contact        /contact(.:format)        {:controller=>"pages", :action=>"contact"}
      about        /about(.:format)          {:controller=>"pages", :action=>"about"}
       help        /help(.:format)           {:controller=>"pages", :action=>"help"}
       root        /(.:format)               {:controller=>"pages", :action=>"home"}

Here are the routes from routes.rb:

  resources :users
  resources :sessions, :only => [:new, :create, :destroy]


  match '/signup', to: 'users#new'
  match '/contact', to: 'pages#contact'
  match '/about', to: 'pages#about'
  match '/help', to: 'pages#help'

4 个答案:

答案 0 :(得分:10)

您可能没有在路线中传递:id 参数,这就是路线不匹配的原因,因为:id 是必需的:

session DELETE /sessions/:id(.:format)   {:action=>"destroy", :controller=>"sessions"}

注意:格式参数周围的括号表示它是可选的。

答案 1 :(得分:8)

我也得到了同样的错误。但原因是我写的视图页面中的小错误      <%= form_for(:session,url:session_path) do |f| %>   我减去了'会话'的最后一个'。

答案 2 :(得分:2)

这看起来像是我在http://ruby.railstutorial.org/中运行时遇到的错误,结果发现我在routes.rb中留下了一些东西。增加资源路线的同时还有以下两条路线:

match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'

很难看到,因为该组中的第一条路线已经存在,所以我刚刚在那里(已经好几次)掩饰了已经存在的那些路线。

答案 3 :(得分:-1)

resources controller将地图添加到方法

{:action=>"method", :controller=>"controller"}

在您的情况下,rails似乎明确要求将地图作为

{:controller=>“controller”, :action=>“method”}

:controller:action之前

这也回答了Noach的问题,为什么match '/signout', :to => 'sessions#destroy'必须存在,如果你耙:你会看到它添加的路线

{:controller=>“sessions”, :action=>“destroy”}虽然已经有了 {:action=>“destroy”, :controller=>“sessions”}

添加了resources sessions