Ruby-on-rails 如何重定向回我当前所在的页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8952487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:40:12  来源:igfitidea点击:

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

ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1rubygems

提问by LondonGuy

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 the photo is stored in the photo_idcolumn of my profiles table.

在我的用户相册页面中,他们会看到他们上传的照片,每张照片上都有一个“设为默认”链接。当用户单击 时make default,照片的 id 将存储在photo_id我的个人资料表的列中。

The issue is redirecting them back to:

问题是将它们重定向回:

localhost:3000/settings/photo_gallery/:id

Is there a way I can redirect back to the photo album using the id of the photo that has just been set as the default? Can Rails find what album I want to redirect to just by looking at the id of a photo, as a photo belongs to a photo album, and a photo album has many photos?

有没有办法可以使用刚刚设置为默认值的照片 ID 重定向回相册?Rails可以通过查看照片的id来找到我想要重定向到的相册,因为照片属于相册,而相册有很多照片?

I have the following tables in my database:

我的数据库中有以下表格:

  • User(s): has one profile, has many PhotoAlbums
  • Profile(s): belongs to user
  • PhotoAlbum(s): belongs to user, has many photos
  • Photo(s): belongs to PhotoAlbum
  • 用户:有一个个人资料,有很多相册
  • 个人资料:属于用户
  • PhotoAlbum(s):属于用户,有很多照片
  • 照片:属于相册

Controller action:

控制器动作:

def set_default_profile_photo

  photo = Profile.find_by_user_id(current_user.id)
  photo.photo_id = params[:photo_id]
  photo.save

  redirect_to "**HERE IS WHERE I'D LIKE TO REDIRECT TO THE PHOTOALBUM THE PHOTO IS IN**"
  flash[:success] = "Default photo set!"

end

My routes:

我的路线:

                    users GET    /users(.:format)                                  {:action=>"index", :controller=>"users"}
                          POST   /users(.:format)                                  {:action=>"create", :controller=>"users"}
                 new_user GET    /users/new(.:format)                              {:action=>"new", :controller=>"users"}
                edit_user GET    /users/:id/edit(.:format)                         {:action=>"edit", :controller=>"users"}
                     user GET    /users/:id(.:format)                              {:action=>"show", :controller=>"users"}
                          PUT    /users/:id(.:format)                              {:action=>"update", :controller=>"users"}
                          DELETE /users/:id(.:format)                              {:action=>"destroy", :controller=>"users"}
                 sessions GET    /sessions(.:format)                               {:action=>"index", :controller=>"sessions"}
                          POST   /sessions(.:format)                               {:action=>"create", :controller=>"sessions"}
              new_session GET    /sessions/new(.:format)                           {:action=>"new", :controller=>"sessions"}
             edit_session GET    /sessions/:id/edit(.:format)                      {:action=>"edit", :controller=>"sessions"}
                  session GET    /sessions/:id(.:format)                           {:action=>"show", :controller=>"sessions"}
                          PUT    /sessions/:id(.:format)                           {:action=>"update", :controller=>"sessions"}
                          DELETE /sessions/:id(.:format)                           {:action=>"destroy", :controller=>"sessions"}
                passwords GET    /passwords(.:format)                              {:action=>"index", :controller=>"passwords"}
                          POST   /passwords(.:format)                              {:action=>"create", :controller=>"passwords"}
             new_password GET    /passwords/new(.:format)                          {:action=>"new", :controller=>"passwords"}
            edit_password GET    /passwords/:id/edit(.:format)                     {:action=>"edit", :controller=>"passwords"}
                 password GET    /passwords/:id(.:format)                          {:action=>"show", :controller=>"passwords"}
                          PUT    /passwords/:id(.:format)                          {:action=>"update", :controller=>"passwords"}
                          DELETE /passwords/:id(.:format)                          {:action=>"destroy", :controller=>"passwords"}
                 profiles GET    /profiles(.:format)                               {:action=>"index", :controller=>"profiles"}
                          POST   /profiles(.:format)                               {:action=>"create", :controller=>"profiles"}
              new_profile GET    /profiles/new(.:format)                           {:action=>"new", :controller=>"profiles"}
             edit_profile GET    /profiles/:id/edit(.:format)                      {:action=>"edit", :controller=>"profiles"}
                  profile GET    /profiles/:id(.:format)                           {:action=>"show", :controller=>"profiles"}
                          PUT    /profiles/:id(.:format)                           {:action=>"update", :controller=>"profiles"}
                          DELETE /profiles/:id(.:format)                           {:action=>"destroy", :controller=>"profiles"}
                   emails GET    /emails(.:format)                                 {:action=>"index", :controller=>"emails"}
                          POST   /emails(.:format)                                 {:action=>"create", :controller=>"emails"}
                new_email GET    /emails/new(.:format)                             {:action=>"new", :controller=>"emails"}
               edit_email GET    /emails/:id/edit(.:format)                        {:action=>"edit", :controller=>"emails"}
                    email GET    /emails/:id(.:format)                             {:action=>"show", :controller=>"emails"}
                          PUT    /emails/:id(.:format)                             {:action=>"update", :controller=>"emails"}
                          DELETE /emails/:id(.:format)                             {:action=>"destroy", :controller=>"emails"}
                     root        /                                                 {:controller=>"users", :action=>"new"}
                  success        /success(.:format)                                {:action=>"success", :controller=>"users"}
                    login        /login(.:format)                                  {:action=>"new", :controller=>"sessions"}
                   logout        /logout(.:format)                                 {:action=>"destroy", :controller=>"sessions"}
           reset_password        /reset_password(.:format)                         {:action=>"new", :controller=>"passwords"}
       setup_new_password        /setup_new_password(.:format)                     {:action=>"edit", :controller=>"passwords"}
                 settings        /settings(.:format)                               {:action=>"settings", :controller=>"users"}
         settings_account        /settings/account(.:format)                       {:controller=>"users", :action=>"account"}
    settings_edit_profile        /settings/edit_profile(.:format)                  {:controller=>"profiles", :action=>"edit_profile"}
                                 /:username(.:format)                              {:controller=>"users", :action=>"show"}
          change_password        /change_password(.:format)                        {:action=>"change_password", :controller=>"users"}
profile_photo_set_default        /profile_photo/set_default(.:format)              {:controller=>"photo_albums", :action=>"set_default_profile_photo"}
  album_photo_set_default        /album_photo/set_default(.:format)                {:controller=>"photo_albums", :action=>"set_default_album_photo"}
             photo_albums GET    /settings/photo_gallery(.:format)                 {:action=>"index", :controller=>"photo_albums"}
                          POST   /settings/photo_gallery(.:format)                 {:action=>"create", :controller=>"photo_albums"}
          new_photo_album GET    /settings/photo_gallery/new(.:format)             {:action=>"new", :controller=>"photo_albums"}
         edit_photo_album GET    /settings/photo_gallery/:id/edit(.:format)        {:action=>"edit", :controller=>"photo_albums"}
              photo_album GET    /settings/photo_gallery/:id(.:format)             {:action=>"show", :controller=>"photo_albums"}
                          PUT    /settings/photo_gallery/:id(.:format)             {:action=>"update", :controller=>"photo_albums"}
                          DELETE /settings/photo_gallery/:id(.:format)             {:action=>"destroy", :controller=>"photo_albums"}
                   photos GET    /settings/photo_gallery/photos(.:format)          {:action=>"index", :controller=>"photos"}
                          POST   /settings/photo_gallery/photos(.:format)          {:action=>"create", :controller=>"photos"}
                new_photo GET    /settings/photo_gallery/photos/new(.:format)      {:action=>"new", :controller=>"photos"}
               edit_photo GET    /settings/photo_gallery/photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"}
                    photo GET    /settings/photo_gallery/photos/:id(.:format)      {:action=>"show", :controller=>"photos"}
                          PUT    /settings/photo_gallery/photos/:id(.:format)      {:action=>"update", :controller=>"photos"}
                          DELETE /settings/photo_gallery/photos/:id(.:format)      {:action=>"destroy", :controller=>"photos"}

回答by

This is what you want:

这就是你想要的:

redirect_to request.referrer

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

进一步参考:http: //en.wikipedia.org/wiki/HTTP_referer

回答by LondonGuy

redirect_to :backworked 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

redirect_to :back对我来说有效,但我想看看这是否是正确的选择 http://api.rubyonrails.org/files/actionpack/lib/action_controller/metal/redirecting_rb.html

回答by evedovelli

In Rails 5 it was introduced the function:

在 Rails 5 中引入了这个函数:

redirect_back(fallback_location: root_path)

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

只要HTTP_REFERER已知,它就会重定向回来。否则它会重定向到fallback_location.

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

redirect_to :back被废弃,将从导轨5.1来除去。

回答by Bjoernsen

In one project we used the session for temporary storage, because redirect_to :backdid not work for us. We had an def newwhere we set session[:return_to] = request.refererin the def createwe added redirect_to session[:return_to]. I do not know anymore, why we could not use redirect_to :back

在一个项目中,我们使用会话作为临时存储,因为redirect_to :back对我们不起作用。我们有一个def new我们session[:return_to] = request.refererdef create我们添加的地方设置的redirect_to session[:return_to]。我不知道了,为什么我们不能使用redirect_to :back

回答by redronin

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

如果您来自 photo_album 页面,您应该能够:

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.

顺便说一句,为什么你有 photo_albums 映射到 photo_galleries?如果您以类似的方式命名资源和路由,就不会那么令人困惑了。即:如果您希望您的路由端点使用 /photo_galleries,您应该将您的资源命名为 PhotoGallery。

回答by Hussain Akram

There is also this handy way to deal with it.

还有这个方便的方法来处理它。

render :nothing => true