ruby 2.5.1 Rails 5.2.4.4 Creating a Ruby on Rails video posting application for a school assignment.
** Logout ** with Devise feature.
app/views/layouts/application.html/erb
<% if user_signed_in? %>
<div class="collapse navbar-collapse" id="Navber">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<%= link_to 'My page', user_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to 'New post', new_movie_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to 'Post list', movies_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Log out", destroy_user_session_path, method: :delete, class: "nav-link" %>
</li>
</ul>
</div>
** 1. ** The route setting of the delete method seems to be an error. Check with rails routes.
rails routes
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
that? The method is ** GET **!
** 2. ** Just in case, check the rails routes of other apps with devise for reference (this time, we will call it "sample").
rails routes(sample)
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
It's ** DELETE **!
** 3. ** What's the difference? Check routes.rb!
routes.rb(sample)
delete 'tweets/:id' => 'tweets#destroy'
routes.rb (code being created)
resource :user, except: [:new, :create, :destroy]
...。 The sample is described by delete.
The code being created uses the resource method. And except: [: ** destroy **] ????
** 4. ** First, delete destroy of except: [: ** destroy **].
Error routes.rb
resource :user, except: [:new, :create]
But when I log out, I get the same error.
** 5. ** Add the following! I think it's double with resourse ...
Error routes.rb
delete 'users/:id' =>'users#destroy'
** 6. ** The error has changed! Only one line error.
** 7. ** Error says: "Cannot find destroy action for UsersController." Added the following and ** solved (no error, go to top screen after logging out!) **!
https://railstutorial.jp/chapters/log_in_log_out?version=4.2#sec-logging_out
app/controllers/users_controller.rb
def destroy
session.delete(:user_id)
@current_user = nil
redirect_to root_url
** 8. ** Check rails routes just in case.
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
Hmm~. After all the method remained GET. There is a feeling of haze, but no error will occur, so we will resolve it.
I would be grateful if anyone could tell me this point (** Why can I log out even with GET? **)! !!
** Wrong (complete misunderstanding): **
** Learning: **
Reference: * Ruby on Rails Tutorial Chapter 8 Section 3 "Logout"
https://railstutorial.jp/chapters/log_in_log_out?version=4.2#sec-logging_out
That's it!
I hope it helps you solve the error!
Recommended Posts