Ruby on rails 3 根据用户属性设计-签出\u后\u路径\u

Ruby on rails 3 根据用户属性设计-签出\u后\u路径\u,ruby-on-rails-3,devise,Ruby On Rails 3,Devise,在早期版本的Desive(3.2)中,可以在(资源)方法的“注销路径”之后访问刚刚在中注销的用户,并根据该用户的任何给定属性重定向 在Desive 3.4中,在为(资源或范围)注销路径之后的方法只接收类名作为参数资源或范围的符号,即在我的例子中是:user 在基于用户模型中属性的值注销后,是否有其他方法重定向给定用户 澄清一下:我不打算创建不同的用户类,只是为了让它工作。我不久前遇到了完全相同的问题,我找不到一种干净的方法。所以为了解决这个问题,我只做了一个简单的覆盖来设计SessionCont

在早期版本的Desive(3.2)中,可以在(资源)方法的“注销路径”之后访问刚刚在
中注销的用户,并根据该用户的任何给定属性重定向

在Desive 3.4中,在为(资源或范围)
注销路径之后的方法
只接收类名作为参数
资源或范围的符号,即在我的例子中是
:user

在基于用户模型中属性的值注销后,是否有其他方法重定向给定用户


澄清一下:我不打算创建不同的用户类,只是为了让它工作。

我不久前遇到了完全相同的问题,我找不到一种干净的方法。所以为了解决这个问题,我只做了一个简单的覆盖来设计SessionController,在设计擦除之前,我会在session中保存所需的属性:

class SessionsController < Devise::SessionsController
  def sign_out
    company_slug = session[:company_slug]
    super
    session[:company_slug] = company_slug
  end
end
当然,您不需要重写signout方法,只需使用


谢谢你给我指明了正确的方向。你的答案在真正起作用之前确实需要一些调整

覆盖的方法实际上是
销毁
(而不是
注销
)。当我用
super
调用它时,它仍然不起作用,因为在设置
会话[:noponsor]
之前,原始会话控制器在
注销之后重定向到
。所以我不得不重写整个方法

class SessionsController < Devise::SessionsController
  def destroy
    nosponsor = current_user && current_user.sponsor.blank?
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
    set_flash_message :notice, :signed_out if signed_out && is_flashing_format?
    yield if block_given?
    session[:nosponsor] = "true" if nosponsor
    respond_to do |format|
      format.all { head :no_content }
      format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
    end
  end
end

我相信你的回答。因此,如果您更新它以合并这些更改,我将接受它作为正确的。再次使用Thx。

我也有同样的需求,我采取了不同的方法(因为注销的行为会清除会话)——使用实例变量而不是会话来存储用户

class SessionsController < Devise::SessionsController
  def destroy
    @user = current_user
    super
  end
end

class ApplicationController < ActionController::Base
  def after_sign_out_path_for(resource)
    if @user.some_method?
       path_a
    else
       path_b
    end
  end
end

我只需在
方法的注销后覆盖路径。我不必重写
destroy
方法,它成功了。我正在使用Desive 4.7.3。
devise_for :users, :controllers =>  {:sessions =>  "sessions"}
class SessionsController < Devise::SessionsController
  def destroy
    @user = current_user
    super
  end
end

class ApplicationController < ActionController::Base
  def after_sign_out_path_for(resource)
    if @user.some_method?
       path_a
    else
       path_b
    end
  end
end
devise_for :users, controllers: {sessions: "sessions"}