Skip to content

Instantly share code, notes, and snippets.

@diegobarros0701
Created May 16, 2018 00:30
Show Gist options
  • Save diegobarros0701/5044cb6b11e39b63119992699f01a05f to your computer and use it in GitHub Desktop.
Save diegobarros0701/5044cb6b11e39b63119992699f01a05f to your computer and use it in GitHub Desktop.
Session management rails
class ApplicationController < ActionController::Base
before_action :validate_session
def validate_session
if session[:user].nil?
redirect_to new_session_path if controller_name != 'sessions'
else
redirect_to home_index_path if controller_name == 'sessions' && action_name != 'destroy'
end
end
end
class SessionsController < ApplicationController
def new
end
def create
session[:user] = { usuario: params[:usuario] }
redirect_to home_index_path
end
def destroy
session.delete(:user)
redirect_to new_session_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment