Ruby on rails 3 Rails3、Authlogic和authenticates\u many如何编写当前\u帐户\u会话助手方法?
启动信息:Ruby on rails 3 Rails3、Authlogic和authenticates\u many如何编写当前\u帐户\u会话助手方法?,ruby-on-rails-3,cookies,authlogic,scope-identity,Ruby On Rails 3,Cookies,Authlogic,Scope Identity,启动信息: 我的系统没有用户子域来获得正确的帐户 我使用Rails 3.0.x 我使用authlogic 2.1.6 模型帐户和模型用户 cookie中有名称,例如帐户\用户\凭据,没错 模型帐户.rb class Account < ActiveRecord::Base authenticates_many :user_sessions, :scope_cookies => true has_many :users end 但是会话\u controller.rb(设
- 我的系统没有用户子域来获得正确的帐户李>
- 我使用Rails 3.0.x
- 我使用authlogic 2.1.6
- 模型帐户和模型用户
- cookie中有名称,例如帐户\用户\凭据,没错李>
class Account < ActiveRecord::Base
authenticates_many :user_sessions, :scope_cookies => true
has_many :users
end
但是会话\u controller.rb(设置当前帐户\u会话)和应用程序\u controller.rb(def当前帐户\u会话的实现…结束)看起来如何?如果所有用户都获得相同的登录,则需要根据当前用户查找帐户。为此,您不需要在许多帐户中使用身份验证。只需验证您的用户,然后获取其帐户 要设置控制器,请查看示例 注意:您也可以检查视图。。。寻找更多的灵感 这将允许您对用户进行身份验证并管理其会话。 登录后,您需要能够获取他的帐户,这样您就可以为每个帐户确定其他请求的范围 要实现这一点,请通过将以下内容添加到应用程序\u controller.rb来添加current\u account helper\u方法
class ApplicationController
helper_method :current_account
private
def current_account
current_user.account
end
memoize :current_account
end
不要忘记添加默认的当前用户和当前用户会话助手方法
这样,您可以在所有控制器中始终找到经过身份验证的用户的当前帐户
class ApplicationController
helper_method :current_user_session, :current_user
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
end
class ApplicationController
helper_method :current_account
private
def current_account
current_user.account
end
memoize :current_account
end