Using Devise in Rails 3.2.3 - Cannot sign out

2019-09-17 09:33发布

问题:

I am using devise 2.1.0 for my rails app 3.2.3. Here is what I did:

Add gem 'devise' to Gemfile and run bundle
Run rails g devise:install
Run rails g devise User
Setup db by running rake db:migrate

The application.html.erb is something like this:

<%if user_signed_in? %>
    Welcome <%= current_user.email%>
    <%= link_to "Sign out", destroy_user_session_path%>
<% else %>
    <%= link_to "Sign up", new_user_registration_path %> or
    <%= link_to "Sign in", new_user_session_path %> 
<% end %>

The sign in and sign up work just fine but When I click Sign out, I got an error message:

Routing Error

No route matches [GET] "/users/sign_out" Try running rake routes for more information on available routes.

Here is the output of the rake routes:

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)                           devise/registrations#cancel
       user_registration POST   /users(.:format)                                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                             devise/registrations#edit
                         PUT    /users(.:format)                                  devise/registrations#update
                         DELETE /users(.:format)                                  devise/registrations#destroy

I am really confused as there are /usrs/sign_out routes in there. Does anyone know why?

回答1:

Try this:

<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>