I'm trying to redirect users back to a page that they were trying to access prior to logging in (the page is only viewable to users).
I'll start off with a before filter that targets my trip new action:
before_filter :authenticate, :only => [:new, :create, :edit, :update, :destroy]
def authenticate
deny_access unless signed_in?
end
def deny_access
store_location
redirect_to login_path, :notice => "Please log in to access this page."
end
def store_location
session[:return_to] = request.fullpath
end
Here is the new and create actions in my sessions_controller.rb
def new
@title = "Log in"
end
def create
user = User.authenticate(params[:session][:email].downcase,
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Log in"
render 'new'
else
sign_in user
redirect_back_or(root_path)
end
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
Thanks!
Edit: Removed MaddHacker's version of my code as that is not what I want.