设计路由错误:没有路由匹配[GET]“/ users / sign_out”

时间:2011-10-10 22:06:23

标签: ruby-on-rails ruby ruby-on-rails-3.1

routes.rb =>

  devise_for :users

  resources :posts
  match "posts/:id/categ" => "posts#categ"
  match "posts/:id/tag_posts" => "posts#tag_posts"
  match "posts/searcharchive" => "posts#searcharchive"
  #post "posts/searcharchive"

  resources :categories
  resources :comments
  resources :countpages

rake routes =>

        new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
                   posts GET    /posts(.:format)               {:action=>"index", :controller=>"posts"}
                         POST   /posts(.:format)               {:action=>"create", :controller=>"posts"}
                new_post GET    /posts/new(.:format)           {:action=>"new", :controller=>"posts"}
               edit_post GET    /posts/:id/edit(.:format)      {:action=>"edit", :controller=>"posts"}
                    post GET    /posts/:id(.:format)           {:action=>"show", :controller=>"posts"}
                         PUT    /posts/:id(.:format)           {:action=>"update", :controller=>"posts"}
                         DELETE /posts/:id(.:format)           {:action=>"destroy", :controller=>"posts"}
                                /posts/:id/categ(.:format)     {:controller=>"posts", :action=>"categ"}
                                /posts/:id/tag_posts(.:format) {:controller=>"posts", :action=>"tag_posts"}
     posts_searcharchive        /posts/searcharchive(.:format) {:controller=>"posts", :action=>"searcharchive"}
              categories GET    /categories(.:format)          {:action=>"index", :controller=>"categories"}
                         POST   /categories(.:format)          {:action=>"create", :controller=>"categories"}
            new_category GET    /categories/new(.:format)      {:action=>"new", :controller=>"categories"}
           edit_category GET    /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
                category GET    /categories/:id(.:format)      {:action=>"show", :controller=>"categories"}
                         PUT    /categories/:id(.:format)      {:action=>"update", :controller=>"categories"}
                         DELETE /categories/:id(.:format)      {:action=>"destroy", :controller=>"categories"}
                comments GET    /comments(.:format)            {:action=>"index", :controller=>"comments"}
                         POST   /comments(.:format)            {:action=>"create", :controller=>"comments"}
             new_comment GET    /comments/new(.:format)        {:action=>"new", :controller=>"comments"}
            edit_comment GET    /comments/:id/edit(.:format)   {:action=>"edit", :controller=>"comments"}
                 comment GET    /comments/:id(.:format)        {:action=>"show", :controller=>"comments"}
                         PUT    /comments/:id(.:format)        {:action=>"update", :controller=>"comments"}
                         DELETE /comments/:id(.:format)        {:action=>"destroy", :controller=>"comments"}
              countpages GET    /countpages(.:format)          {:action=>"index", :controller=>"countpages"}
                         POST   /countpages(.:format)          {:action=>"create", :controller=>"countpages"}
           new_countpage GET    /countpages/new(.:format)      {:action=>"new", :controller=>"countpages"}
          edit_countpage GET    /countpages/:id/edit(.:format) {:action=>"edit", :controller=>"countpages"}
               countpage GET    /countpages/:id(.:format)      {:action=>"show", :controller=>"countpages"}
                         PUT    /countpages/:id(.:format)      {:action=>"update", :controller=>"countpages"}
                         DELETE /countpages/:id(.:format)      {:action=>"destroy", :controller=>"countpages"}
                    root        /                              {:controller=>"posts", :action=>"index"}

links =>

            <% if user_signed_in? %>
                <%= link_to('Logout', destroy_user_session_path, :method => 'delete') %>                
                <%= link_to 'Add Post', :controller=>'/posts', :action=>'new' %>
                <%= link_to 'Add Category', :controller=>'/categories', :action=>'new' %>
                <%= link_to 'Display ategories', :controller=>'/categories', :action=>'index' %>
            <% else %>
                <%= link_to('Login', new_user_session_path)  %>
            <% end %>

http://0.0.0.0:3000/users/sign_out =&gt;

错误=&gt;

Routing Error

No route matches [GET] "/users/sign_out"

我该怎么办?

2 个答案:

答案 0 :(得分:4)

根据其定义,您正在使用GET调用路由,而路由需要DELETE。

destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}

链接正确使用DELETE

<%= link_to('Logout', destroy_user_session_path, :method => 'delete') %>

您是否尝试直接在浏览器中访问该网址?在这种情况下,它将无法正常工作。您需要使用DELETE方法单击链接。

答案 1 :(得分:1)

你可以尝试这样的事情 以下代码应该有效

我最诚挚的问候

devise_for :users, :skip => [:sessions]
as :user do
    get 'signin' => 'devise/sessions#new', :as => :new_user_session
    post 'signin' => 'devise/sessions#create', :as => :user_session
    get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end