I have a sign in form in my application nav bar and I was wondering if there is an easy way to have to sign_in redirect to whatever page you are currently on when you sign_in. Thanks!
You can redirect_to(:back)
, as long as a referrer is given. However, I'd recommend that you take a look at this answer , as the solution presented there will be resilient to things like failed logins and OAuth redirects.
在Rails 5中,您应该使用redirect_back
并添加回退路径:
redirect_back(fallback_location: root_path)
If you have written the code to create a session for your user ,then simply add this line after that in your sessions_controller:
redirect_to root_url, :notice => "Logged in!"
here instead of root_url you can enter any path you wish the user be redirected to. but make sure you have it in you routes.rb
and if you considering the unsuccessful sign_in you can use render
method to reload your sign_in view
This solution from the Devise folks should do the trick...
In ApplicationController
write the following:
after_filter :store_location
def store_location
# store last url - this is needed for post-login redirect to whatever the user last visited.
return unless request.get?
if (request.path != "/users/sign_in" &&
request.path != "/users/sign_up" &&
request.path != "/users/password/new" &&
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)
session[:previous_url] || root_path
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.