Using Devise for Two Different Models but the same

2020-02-08 19:13发布

问题:

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

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.

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.

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.

Thanks!

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

回答1:

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).

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.



回答2:

Can do this in application_controller?

current_user = current_resource_a || current_resource_b