在 Devise (RoR) 中创建基于多模型的帐户创建表单的最佳方式是什么
编辑注释 现在我对 Rails 和 Rails 有了更好的理解,我正在完全重写这个问题。设计。
我正在寻找一种利用单个表结构(帐户)来创建各种帐户类型的方法。
我现在遇到的困难是一个结构,我需要我的企业有一个帐户,但不一定反之亦然(帐户可能只是一个典型的用户)。我认为最简单的方法就是建立一对一的关系,而不是继承,但我可能会弄错。
让我感到困惑的原因是注册过程。如果我接受帐户信息,我相信我可以使用 accepts_nested_attributes_for接受帐户信息,但恐怕这会破坏设计所期望的工作流程。我考虑过重写 Devise::RegistrationController
但我真的不知道 Rails 将如何处理这个问题(即,如果我调用 super 但我正在处理业务而不是帐户 - 会发生什么? )
EDIT NOTE
I am rewording this question entirely now that I have a bit better understanding of rails & devise.
I am looking for a way to utilize a single table structure (Account) to create various account types.
What I am now having a hard time with is a structure where I need my Business to have an account but not necessarily vice versa (an Account could just be a typical user). I think the easiest approach would be just to have a 1 to 1 relation as opposed to inheritance but I could be mistaken there.
The reason its confusing to me is the registration process. If I accept the account information, I believe I could use accepts_nested_attributes_for to accept the account information but im afraid that'll break the workflow that devise is expecting. I considered overriding Devise::RegistrationController
but I don't really know how rails is going to handle that (ie, if I call super but I am dealing with a Business rather than an Account - what happens?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 CanCan 来创建帐户角色,并在代码中询问
current_user.role?(:admin)
有一个很好的集成了 device/cancan/spike 的应用程序模板:
https://github.com/augusto/devise-cancan-spike
You can use CanCan to make account roles, and ask in your code
current_user.role?(:admin)
There is good app template with device/cancan/spike integrated:
https://github.com/augusto/devise-cancan-spike
只要模型彼此相关,拥有一个管理多个模型的表单本身就没有问题。
实现此目的的“库存”方法是在模型中使用“accepts_nested_attributes_for”。
对于你的情况,你会做这样的事情:
然后在你的注册视图中,你会使用:
如果你想以相同的形式处理员工和“普通用户”注册,你可能会做这样的事情(从未尝试过)这个,但我认为它应该有效!):
PS你在问题中提到你担心 Devise 无法处理嵌套属性。确实如此,正如我在我的一个应用程序中所做的那样。
There is no problem per-se with having a form which manages multiple models, so long as the models are related to one another.
The 'stock' way of achieving this would be to use 'accepts_nested_attributes_for' in your model.
For your situation, you'd do something like this:
Then in your registration view, you would use:
If you wanted to handle both employee and 'normal user' registration in the same form, you could probably do something like this (never tried this, but I think it should work!):
P.S. you mentioned in your question that you were worried Devise wouldn't cope with nested attributes. It definitely does, as I do exactly this in one of my applications.