Created
May 17, 2018 06:49
-
-
Save 3h5a9/d6697564b1d722371aef9d5f9c67ceae to your computer and use it in GitHub Desktop.
Problem with login and logout link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::Base | |
| helper_method :current_user, :logged_in? | |
| def current_user | |
| @current_user ||= User.find(session[:user_id]) if session[:user_id] | |
| end | |
| def logged_in? | |
| !!current_user | |
| end | |
| def require_user | |
| if !logged_in? | |
| flash[:danger] = "You must be logged in to perform that action." | |
| redirect_to root_path | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SessionsController < ApplicationController | |
| def new | |
| end | |
| def create | |
| user = User.find_by(email: params[:session][:email].downcase) | |
| if user && user.authenticate(params[:session][:password]) | |
| session[:user_id] | |
| flash[:success] = "You have successfully logged in." | |
| redirect_to user_path(user) | |
| else | |
| flash.now[:danger] = "There was something wrong with the login information." | |
| render 'new' | |
| end | |
| end | |
| def destroy | |
| session[:user_id] = nil | |
| flash[:success] = "You have logged out." | |
| redirect_to root_path | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment