更新后设计更改重定向

Phi*_*899 2 ruby model-view-controller ruby-on-rails devise ruby-on-rails-4

我已经尝试过 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
Run Code Online (Sandbox Code Playgroud)

当我检查我的服务器时,会出现 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

Phi*_*899 5

这是我解决问题的方法:

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
Run Code Online (Sandbox Code Playgroud)

路线:

devise_for :users, :controllers => { :registrations => :registrations }
Run Code Online (Sandbox Code Playgroud)

唯一的问题是,如果更改密码成功,这只会执行重定向。如果没有,重定向不会发生。有谁知道如何做到这一点,以便在出现错误时也会发生重定向?


mur*_*urb 5

从文档:

(Object) after_update_path_for(resource) (protected)
更新资源后使用的默认 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
Run Code Online (Sandbox Code Playgroud)

虽然一开始对我来说似乎有点骇人听闻,但我相信它很“好”;用户范围的根路径确实可能是仪表板页面。


Ish*_*pta 0

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

def after_update_path_for(resource_or_scope)
  dashboard_url
end
Run Code Online (Sandbox Code Playgroud)