我有一个多租户应用程序,我像这样设置当前租户:
class ApplicationController < ActionController::Base
around_filter :scope_current_tenancy
def scope_current_tenancy
Tenancy.current_id = current_tenancy.id if request.subdomain != 'www'
yield
ensure
Tenancy.current_id = nil
end
end然后,在我的用户模型中,我定义了一个default_scope,只允许租户内的用户访问:
class Postulant < ActiveRecord::Base
default_scope ->{ where("enlistments.tenancy_id = ?", Tenancy.current_id).includes(:enlistments).references(:enlistments) }到目前为止,这是有效的,但现在使用devise_invitable并尝试接受邀请,我收到了一条Filter chain halted as :resource_from_invitation_token rendered or redirected消息。这个问题是因为我的scope_current_tenancy过滤器是在resource_from_invitation_token之后执行的,所以resource没有正确加载。
class Devise::InvitationsController < DeviseController
prepend_before_filter :resource_from_invitation_token, :only => [:edit, :destroy]
def resource_from_invitation_token
# Here 'resource_class' is my Postulant model, so when I call
# 'find_by_invitation_token' applies the defined default_scope
# which doesn't word without 'scope_current_tenancy'
unless params[:invitation_token] && self.resource = resource_class.find_by_invitation_token(params[:invitation_token], true)
set_flash_message(:alert, :invitation_token_invalid)
redirect_to after_sign_out_path_for(resource_name)
end
end
end所以我的问题是,有没有比:resource_from_invitation_token更早运行:scope_current_tenancy的方法
我曾尝试将around_filter :scope_current_tenancy更改为prepend_around_filter :scope_current_tenancy,但没有成功。有什么想法吗?
发布于 2013-11-20 12:09:01
因为prepend_before_filter :resource_from_invitation_token位于ApplicationController之后,所以即使您对scope_current_tenancy使用prepend_before_filter,此筛选器也会放在筛选器链的前面。一种选择可能是尝试如下所示:
skip_around_filter :scope_current_tenancy
prepend_before_filter :resource_from_invitation_token, :only => [:edit, :destroy]
prepend_around_filter :scope_current_tenancy在你的设计中::InvitationsController
不确定这是否会奏效,但似乎值得一试。
或者,你可以去掉'skip_around_filter‘行,假设scope_current_tenancy是幂等的,这似乎就是这种情况。
https://stackoverflow.com/questions/20087006
复制相似问题