对两个不同的模型但相同的登录表单使用设计 [英] Using Devise for Two Different Models but the same Login Form

查看:19
本文介绍了对两个不同的模型但相同的登录表单使用设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到了很多类似的问题,但没有一个完全符合我的需要.

I have seen lots of similar questions here but nothing that quite fits my need.

我是一位经验丰富的 Rails 开发人员,但这个新项目是我第一次同时使用 Rails 3 和 Devise(我通常使用 authlogic).

I am a pretty experience rails developer but this new project is my first time using both Rails 3 and Devise (I'm normally on authlogic).

我的应用有两种不同的模型,我想通过设计对其进行身份验证.

My app has two different models that I want to authenticate via devise.

一,User只是一个标准的用户模型二,Business 类似于用户,(它也有一个电子邮件地址栏),但它在数据库中有附加信息(地址、电话号码等).

One, User is just a standard users model Two, Business is similar to a user, (it has an email address column too) but it has additional info in the database (address, phone number, etc..)

我希望能够通过同一个登录表单同时登录它们.然后很明显,一旦他们登录,他们将根据登录的模型类型显示不同的信息.

I want to be able to log them both in via the same login form. Then obviously once they are logged in they will be presented with different info depending on what type of Model has logged in.

我计划使用 OmniAuth 来允许用户(虽然可能不是企业)通过 Facebook 注册/登录,这可能相关,也可能不相关.

It may or may not be relevant that I was planning on using OmniAuth to allow Users (though probably not businesses) to sign up/on via facebook.

谢谢!

执行此操作的最简单方法是什么?

What's the easiest way to go about doing this?

推荐答案

我认为处理这个问题的唯一方法是拥有自己的自定义登录表单和控制器,以确定用户的类型,然后正确登录.为了简单起见,我会推荐一种类似于 mark 提到的方法(看看像 CanCan 之类的东西来管理角色).

I think the only way to handle this would be to have your own custom sign in form and controller that determined the type of user and then sign them in correctly. I would recommend an approach like what mark mentioned for simplicity (take a look at something like CanCan to manage roles).

拥有多个用户模型的另一个潜在问题是您将拥有所有设计助手方法的多个版本.所以对于 current__signed_in? 你会有 current_usercurrent_business_useruser_signed_in?business_user_signed_in?.然后,您要么必须实现这些方法的自己版本,要么需要在使用它们的任何地方检查这两个版本.

Another potential problem with having multiple user models is that you will have multiple versions of all the devise helper methods. So for current_<resource> and <resource>_signed_in? you would have current_user, current_business_user, user_signed_in? and business_user_signed_in?. Then you would either have to implement your own versions of these methods or you would need to check both versions everywhere you used them.

这篇关于对两个不同的模型但相同的登录表单使用设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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