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

查看:55
本文介绍了设计中多个模型上的相同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 帮助器。对于志愿者来说,这失败了,因为设计为志愿者模型创建了 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 => 用户 ,这会创建一个引用用户和志愿者的current_user,但是现在的问题是志愿者的路线被弄乱了。

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 助手,请参考其他模型除了用户以外?

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

推荐答案

我认为您可以通过这种方式允许用户以用户身份和Voluteer身份登录会议。

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天全站免登陆