设计中多个模型上的相同 current_user [英] The same current_user on multiple models in devise

查看:18
本文介绍了设计中多个模型上的相同 current_user的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 User 模型,以及继承自 User 模型的 Volunteer 模型:

I have a User model, and the Volunteer model that inherits from the User model:

class User < ActiveRecord::Base
end

class Volunteer < User
end

它们都保存在数据库中的同一个表中,但具有不同的控制器/路由.

They are both saved on the same table in the database, but have different controllers / routes.

路线是:

devise_for :users ....
devise_for :volunteers ....

这很好用,但我使用的授权系统依赖于 current_user 助手.这对志愿者来说是失败的,因为 devise 为志愿者模型创建了 current_volunteer.

And this works fine and dandy, but the authorization system that I use depends on a current_user helper. And this fails for volunteers because devise creates current_volunteer for the Volunteer model.

我所尝试的是设置 devise_for :volunteers, :singular =>"user",这样就创建了一个current_user,指的是users和志愿者,但现在的问题是志愿者的路线乱了.

What i have tried is to set devise_for :volunteers, :singular => "user", and this creates a current_user that refers to users and volunteers, but the problem now is that the routes for volunteers are messed up.

所以我的问题是,有没有办法让 current_user 助手引用除 user 之外的另一个模型?

So my question is, is there any way to have the current_user helper, refer to another model other than user?

推荐答案

我认为这样可以让用户在同一会话中以用户和志愿者的身份登录.

I think this way you allow a user to be signed in as a User and a Voluteer in the same session.

一旦你有办法处理它,你就不能拥有

Once you have a way to deal with that could you not just have

# in application_controller.rb
alias_method :devise_current_user, :current_user
def current_user
  devise_current_user || current_volunteer
end

在您的 application_controller.rb 中

in your application_controller.rb

这篇关于设计中多个模型上的相同 current_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆