How do I redirect back to a page I'm currently on?

假装没事ソ 提交于 2019-12-04 01:23:35
LondonGuy

redirect_to :back worked for me but I want to see if this was the right choice http://api.rubyonrails.org/files/actionpack/lib/action_controller/metal/redirecting_rb.html

This is what you want:

redirect_to request.referrer

For further reference: http://en.wikipedia.org/wiki/HTTP_referer

In Rails 5 it was introduced the function:

redirect_back(fallback_location: root_path)

It does redirect back whenever the HTTP_REFERER is known. Otherwise it redirects to the fallback_location.

The redirect_to :back is deprecated and will be removed from Rails 5.1.

In one project we used the session for temporary storage, because redirect_to :back did not work for us. We had an def new where we set session[:return_to] = request.referer in the def create we added redirect_to session[:return_to]. I do not know anymore, why we could not use redirect_to :back

If you came from the photo_album page, you should be able to do:

redirect_to :back

Otherwise, you should be able to do a named route like:

redirect_to photo_album_path(photo.album_id) # or whatever the association key is

BTW, why do you have photo_albums mapping to photo_galleries? It's a lot less confusing if you named your resources and routes in a similiar manner. ie: if you wanted your route endpoints to use /photo_galleries you should name your resource PhotoGallery.

There is also this handy way to deal with it.

render :nothing => true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!