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

后端 未结 6 1792
北荒
北荒 2021-02-12 16:24

In my users photo album page they see photos they have uploaded and each photo has a \'make default\' link on it. When the user clicks make default, then the id of

相关标签:
6条回答
  • 2021-02-12 17:08

    There is also this handy way to deal with it.

    render :nothing => true
    
    0 讨论(0)
  • 2021-02-12 17:15

    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.

    0 讨论(0)
  • 2021-02-12 17:15

    This is what you want:

    redirect_to request.referrer
    

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

    0 讨论(0)
  • 2021-02-12 17:16

    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

    0 讨论(0)
  • 2021-02-12 17:19

    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

    0 讨论(0)
  • 2021-02-12 17:24

    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.

    0 讨论(0)
提交回复
热议问题