用户信息属于公司设计时的Rails设计 [英] Rails devise when the user information belongs to Company devise
问题描述
我为公司设计了一个装置.我已经创建了一个表用户,希望我要存储用户信息密码等.当用户注册后,我希望它创建一个新用户以及与该公司的关联.
I have a created a devise for Companies. I have made a table users, wish I want to store the user information password etc.. When a user sign up I want it to create a new user and a association to the Company.
我的公司型号: has_one:用户 我的用户模型: 属于:公司
My Company model: has_one: User My User model: Belongs_to: Company
我应该如何覆盖注册控制器?
How should I overwrite the registration controller?
最诚挚的问候, Rails初学者
Best regards, Rails beginner
推荐答案
如果我在这里输入的错误,请纠正我,但听起来公司将不会登录-仅属于公司的用户.
Correct me if I'm wrong here, but it sounds like companies won't be logging in — only users, which belong to a company.
请记住,您应该将User
作为Devise模型而不是Company
.那么,您的关联是正确的.下一步将使用嵌套属性(视图中的fields_for
;模型中的accepts_nested_attributes_for
)以company#new
的形式为用户添加字段.
With that in mind, you should have User
as the Devise model instead of Company
. Your associations, then, are correct. The next step would be to add fields for the user in the form for company#new
using nested attributes (fields_for
in the view; accepts_nested_attributes_for
in the model).
回答您的原始问题:您可以使用嵌套属性在注册表单中创建新用户.您不需要重写Devise的注册控制器.只需修改注册表单即可包括fields_for
个用户.另外,请确保Company
模型的User
模型具有accepts_nested_attributes_for
.
Answering your original question: you can use nested attributes to create a new user in the sign up form. You wouldn't need to override Devise's registration controller. Just modify the sign up form to include fields_for
a user. Also, make sure the Company
model has accepts_nested_attributes_for
for the User
model.
简短说明:我不知道您的应用程序的体系结构,但是has_many
似乎更合适.在类似情况下,您很可能希望拥有多个用户属于一个公司.
A quick aside: I'm not aware of the architecture of your application, but it seems like a has_many
would be more appropriate. In cases similar to this, it's far more likely that you'll want to have multiple users belonging to a single company.
这篇关于用户信息属于公司设计时的Rails设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!