【问题标题】:devise change redirect after update更新后设计更改重定向
【发布时间】:2014-02-05 19:25:12
【问题描述】:

我已经尝试过 Devise 的 github 上的代码。在我的应用程序控制器中,我有:

after_filter :store_location

def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
  if (request.fullpath != "/users/sign_in" &&
      request.fullpath != "/users/sign_up" &&
      request.fullpath != "/users/password" ) 
    session[:previous_url] = request.fullpath 
    puts 'stores location'
  end
end

def after_update_path_for(resource)
  session[:previous_url] || dashboard_path
  puts 'after update'
end

当我检查我的服务器时,store_location 方法中的 puts 语句会出现,但 after_update_path_for 中的 puts 语句不会出现。如何让 after_update_redirect 工作?

这是设计说要做的,但它不起作用:

https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

【问题讨论】:

  • 有人有什么想法吗?我想不通。

标签: ruby-on-rails ruby model-view-controller devise ruby-on-rails-4


【解决方案1】:

来自文档:

(对象)after_update_path_for(资源)(受保护)
更新资源后使用的默认 url。您需要在自己的 RegistrationsController 中覆盖此方法。

所以创建你的 ow RegistrationsController 是正确的。不过,这是一个更简单的解决方案:

after_update_path_for 调用signed_in_root_path(resource) 看起来像一个家#{scope}_root_path。这里的范围通常是user(但如果不是,你可能知道它是什么)。对于“用户”,在您的应用程序控制器中实现user_root_path,返回您的dashboard_url,应该可以工作。

def user_root_path
  dashboard_url
end

虽然一开始对我来说有点生硬,但我相信这还不错;用户范围的根路径确实可以是仪表板页面。

【讨论】:

    【解决方案2】:

    这是我解决问题的方法:

    class RegistrationsController < Devise::RegistrationsController
    
        protected
    
        def after_update_path_for(resource)
            puts 'this is happening yoyo mama'
            flash[:notice] = "Account succesfully updated"
            edit_user_registration_path
        end
    end
    

    路线:

    devise_for :users, :controllers => { :registrations => :registrations }
    

    唯一的问题是,如果更改密码成功,这只会进行重定向。如果不是,则不会发生重定向。有谁知道如何做到这一点,所以如果有错误也会发生重定向?

    【讨论】:

      【解决方案3】:

      根据Devise docs,覆盖默认值并添加路由。除非您当然也想更改它,否则无需设置 Flash 消息。

      # Example subclass/override (registrations_controller.rb)
      class Users::RegistrationsController < Devise::RegistrationsController
      
        protected
      
        def after_update_path_for(resource)
          user_path(resource)
        end
      end
      
      # Example routing config (in routes.rb):
      devise_for :users, :controllers => { :registrations => :registrations }
      

      【讨论】:

      • user_path(resource) 对我不起作用,current_user 也不起作用。所以,我简单地使用了:"/users/#{resource.id}"
      【解决方案4】:

      respond_with resource, :location =&gt; after_update_path_for(resource)是更新后设置重定向路径的代码。要更改默认重定向,请通过添加以下代码覆盖应用程序控制器中的以下方法

      def after_update_path_for(resource_or_scope)
        dashboard_url
      end
      

      【讨论】:

      • 谢谢,但这没有用。我不认为该方法被调用。我在里面放了一个 puts 语句,但它从来没有到达服务器。
      【解决方案5】:

      覆盖 ApplicationController 中的路由对我也不起作用,但将其添加到 Users::RegistrationsController 是有效的。 例如,

      class Users::RegistrationsController < Devise::RegistrationsController
      def after_update_path_for(resource)
          current_user
      end
      

      在相关说明中,可以将 after_sign_in_path 添加到 SessionsController

      class Users::SessionsController < Devise::SessionsController
      def after_sign_in_path_for(resource)
        current_user
      end
      

      我的路线如下所示:

      devise_for :users, controllers: {
        confirmations: "users/confirmations",
        passwords: "users/passwords",
        registrations: "users/registrations",
        sessions: "users/sessions",
        unlocks: "users/unlocks",
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多