I'm using Devise to handle user authentication. I've put a sign in form in another controller view. When a user logs in through this form, I want to redirect them back to that page. I've tried accomplishing this with the following:
Application Controller
after_filter :store_location
def store_location
return unless request.get?
if (request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
request.path != "/login" &&
request.path != "/signup" &&
request.path != "/users/password/edit" &&
request.path != "/users/confirmation" &&
request.path != "/users/sign_out" &&
!request.xhr?) # don't store ajax calls
session[:previous_url] = request.fullpath
end
end
def after_sign_in_path_for(resource)
if request.referer == streams_url(page: params[:page])
session[:previous_url] || root_path
else
root_path
end
end
The issue with this code is when a user accesses a link from an external source, or types in the full address of the page, it doesn't work. I need a solution that works every time a user uses this specific form to log in.
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end
add above in application_controller. If last request will be sign in then it will call parent request.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments