【问题标题】:How do I redirect back to a page I'm currently on?如何重定向回我当前所在的页面?
【发布时间】:2012-02-15 16:08:12
【问题描述】:

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

问题是将它们重定向回:

localhost:3000/settings/photo_gallery/:id

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

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

  • 用户:拥有一个个人资料,拥有多个相册
  • 个人资料:属于用户
  • 相册:属于用户,有很多照片
  • 照片:属于相册

控制器动作:

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

我的路线:

                    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"}

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 rubygems


【解决方案1】:

这就是你想要的:

redirect_to request.referrer

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

【讨论】:

    【解决方案2】:

    redirect_to :back 为我工作,但我想看看这是否是正确的选择 http://api.rubyonrails.org/files/actionpack/lib/action_controller/metal/redirecting_rb.html

    【讨论】:

    • 对我来说看起来是正确的。如果您没有使用redirect_to :back 的选项,您可以使用redirect_to PhotoAlbum.where(:id => Photo.where(:id => params[:photo_id]).first.photo_album_id) 获得相同的结果。有更好的方法来做这个查询,但这应该可以。
    【解决方案3】:

    在 Rails 5 中引入了这个功能:

    redirect_back(fallback_location: root_path)
    

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

    redirect_to :back 已弃用,将从 Rails 5.1 中删除。

    【讨论】:

    • 我如何通过redirect_back 语句传递notice 对象,以便告诉用户Flash 消息出了问题?
    • 好吧,我可以这样做:redirect_back(fallback_location: root_path, notice: "Something went wrong!")。希望it could以某种方式提供帮助。
    【解决方案4】:

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

    【讨论】:

    • 你没有使用redirect_to :back的原因很可能是因为你想要返回两个步骤,即如果你从create'返回'你将被带回new,这就是为什么您实际上想要回到new之前的两个步骤。
    【解决方案5】:

    如果您来自 photo_album 页面,您应该可以:

    redirect_to :back
    

    否则,您应该能够执行如下命名路由:

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

    顺便说一句,为什么你将 photo_albums 映射到 photo_galleries?如果您以类似的方式命名您的资源和路线,那么混乱会少很多。即:如果您希望您的路由端点使用 /photo_gallery,您应该将您的资源命名为 PhotoGallery。

    【讨论】:

      【解决方案6】:

      也有这种方便的处理方式。

      render :nothing => true
      

      【讨论】:

      • 但实际上什么都没有呈现
      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 2021-06-19
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多