Skip to content

Instantly share code, notes, and snippets.

@carlweis
Last active May 1, 2017 21:54
Show Gist options
  • Save carlweis/5d24f31fa20aef1626cd9ace2a15c57d to your computer and use it in GitHub Desktop.
Save carlweis/5d24f31fa20aef1626cd9ace2a15c57d to your computer and use it in GitHub Desktop.
Ghosting as a user
# config/routes.rb
resource :ghost, only: [:create, :destroy]
# app/controllers/ghosts_controller.rb
class GhostsController < ApplicationController
def create
session[:admin_id] = current_user.id
user = User.find(params[:user_id])
sign_in user
redirect_to root_path, notice: "Now Ghosting as #{user.email}"
end
def destroy
sign_in User.find(session[:admin_id])
session.delete(:admin_id)
redirect_to admin_path, notice: "Stopped Ghosting"
end
end
# app/controllers/application_controller.rb
def ghosting?
session[:admin_id].present?
end
helper_method :ghosting?
# app/views/layouts/_header_links.html.erb
<%= render "shared/ghosting_link" if ghosting? %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment