Search code examples
ruby-on-railssessionrailstutorial.org

Rails: Michael Hartl Tutorial Chapter 8 - Can't Delete Session


while implementing sessions in Michael Hartl's Ruby on Rails Tutorial (chapter 8), I can't seem to get my link_to tag, which uses method delete, to work.

What I am trying to do is delete the cookie that was created in this chapter, and redirect the user to the home page. I am using Cloud 9/ C9 as an IDE.

This is what comes up on the console/server logs upon clicking the aforementioned link:

Started DELETE "/logout" for 86.42.8.216 at 2017-06-29 12:24:57 +0000
Cannot render console from 86.42.8.216! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SessionsController#delete as HTML
  Parameters: {"authenticity_token"=>"dwH+Eu3big+otJ5nO9dzfJstEVLWSSIr1QrWQQ+g9WD7EdgHkUBVusE3Q1SBHh+SRIOsbNDH8WJwBGGoaZ9Xng=="}
No template found for SessionsController#delete, rendering head :no_content
Completed 204 No Content in 19ms (ActiveRecord: 0.0ms)

Here are my controllers:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params) # Essentially passing in as argument the
                                  # created user's attributes (name, email, etc)
    if @user.save
      # Handle a successful save.
      redirect_to user_url(@user) # Using the named route 'user_URL' method 
                                  # to display show page for users, and passing 
                                  # in the user that was just created.
      flash[:success] = "Welcome to the Sample App!"
      log_in @user # Encrypts the ID of this newly created user. Refer to 
                   # SessionHelper file
    else
      render 'new'
    end
  end

  def destroy
    log_out
    redirect_to root_url
  end


  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end
end

And the routes:

Rails.application.routes.draw do



  get 'sessions/new'

  root 'static_pages#home'
  get '/help', to: 'static_pages#help'
  get  '/about', to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'

  get '/signup', to: 'users#new'
  post '/signup', to: 'users#create'

  get 'login', to: 'sessions#new'
  post 'login', to: 'sessions#create'
  delete 'logout', to: 'sessions#delete'


  # The following resounces method/keyword creates vitural URLs (/users e.g) and
  # maps them to controller actions. This also makes the named routes for 
  # these controllers/actions available

  resources :users

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html


end

And the view (header)

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home", root_path %></li>
        <li><%= link_to "Help", help_path %></li>
        <% if logged_in? %>
          <li><%= link_to "Users", '#' %></li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><%= link_to "Profile", current_user %></li>
              <li><%= link_to "Settings", '#' %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Log out", logout_path, method: :delete %>
              </li>
            </ul>
          </li>
        <% else %>
          <li><%= link_to "Log in", login_path %></li>
        <% end %>
      </ul>
    </nav>
  </div>
</header>

I have searched related questions, the answers of which brought up Rail's application.html.erb file and application.js file so I will post them:

Application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title> <%= full_title(yield (:title)) %> </title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    <!--[if lt IE 9]>
      <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/r29/html5.min.js">
      </script>
    <![endif]-->
  </head>

  <body>

    <!-- Header -->
    <%= render 'layouts/header' %>



    <!-- Body -->
    <div class="container">

      <% flash.each do |message_type, message| %>
        <div class="alert alert-<%= message_type %>">
          <%= message %>
        </div>
      <% end %>


      <%= yield %>
    </div>

    <!-- Footer -->

    <%= render 'layouts/footer' %>

    <!-- Display debug information -->
    <%= debug(params) if Rails.env.development? %>

  </body>
</html>

Application.js

//= require jquery
//= require bootstrap
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Any help or advice is very much appreciated.


Solution

  • Cut your logout method from users controller and paste it to sessions controller

    sessions_controller.rb

     def destroy
      log_out
      redirect_to root_url
     end