为用户和公司设计Cancan注册 [英] Devise Cancan registration for Users and Companies

查看:197
本文介绍了为用户和公司设计Cancan注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails 3应用程序中,我有2个逻辑实体 - 用户和公司。
我想要有两种不同的注册表单(用于用户和公司)。另外,对于他们来说,将会有一个登录表单。



我现在已经配置了两个角色(用户,公司)的Devise + Cancan用户模型,所以我现在有/ users / sign_in和/ users / sign_up。



我想在我的应用程序中有以下url:

  / login 
/ users / signup
/ companies / signup
/ pre>

另一个问题是如何组织用户和公司之间的关系,如果公司继承自用户或我使用聚合 - 用户has_one公司?我更喜欢第二种变体,并计划使用cancan user role =company的用户user.company。



请帮助我。谢谢。

解决方案

您可以在设计中拥有多个模型。您可以添加公司。



rails生成设计公司



这将为您提供您所提到的网址。



关于用户与公司之间的关系通常使用:

  class User< ActiveRecord :: Base 
belongs_to:company
end

class公司< ActiveRecord :: Base
has_many:users
end

您必须添加 company_id 列到迁移中的用户模型,以实现此目的。那时没有遗产。用户和公司被完全分开处理。但是您可以访问 user.company company.users


In Rails 3 application I have 2 logical entities - User and Company. I'd like to have 2 different forms for sign up(for users and for companies). Also, will be great to have one login form for both of them.

What I have now - configured Devise+Cancan for User model with two roles(user, company), so I have now "/users/sign_in" and "/users/sign_up".

I'd like to have following urls in my application:

/login
/users/signup
/companies/signup

One other question is how to organize relationship between User and Company, should the company inherited from User or I shoud use aggregation - User has_one Company ? I prefer second variant and plan to user user.company with cancan user role = "company".

Please help me with it. Thanks.

解决方案

You can have multiple models in devise. You can add a company as well.

rails generate devise company

This will get you the url you mentioned.

Regarding the relationship between User and Company It's common to use:

class User < ActiveRecord::Base
  belongs_to :company
end

class Company < ActiveRecord::Base
  has_many :users
end

You have to add a company_id column to the User model in a migration to make this happen. There is no inheritance then. Users and companies are treated completely separate. But you are able to access user.company and company.users.

这篇关于为用户和公司设计Cancan注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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