在 Devise (RoR) 中创建基于多模型的帐户创建表单的最佳方式是什么

发布于 2024-10-26 00:21:35 字数 504 浏览 0 评论 0原文

编辑注释 现在我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烦人精 2024-11-02 00:21:35

您可以使用 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

冷默言语 2024-11-02 00:21:35

只要模型彼此相关,拥有一个管理多个模型的表单本身就没有问题。

实现此目的的“库存”方法是在模型中使用“accepts_nested_attributes_for”。

对于你的情况,你会做这样的事情:

class Employee < ActiveRecord::Base
  belongs_to :business
  accepts_nested_attributes_for :business
end

然后在你的注册视图中,你会使用:

<!-- validation errors etc -->
<%= form_for @employee do |f| %>
  <!-- all your employee fields etc -->

  <%= f.fields_for :business do |b| %>
    <p>
      <%= b.label :name %>
      <br/>
      <%= b.text_field :name %>
    </p>

    <!-- more fields from business -->
  <% end %>
<% end %>

如果你想以相同的形式处理员工和“普通用户”注册,你可能会做这样的事情(从未尝试过)这个,但我认为它应该有效!):

<!-- validation errors etc -->
<%= form_for @person do |f| %>
  <!-- all your person fields etc, assuming no extras for employee -->

  <% if @person.respond_to? :business %>
    <%= f.fields_for :business do |b| %>
      <p>
        <%= b.label :name %>
        <br/>
        <%= b.text_field :name %>
      </p>

      <!-- more fields from business -->
    <% end %>
  <% end %>
<% end %>

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:

class Employee < ActiveRecord::Base
  belongs_to :business
  accepts_nested_attributes_for :business
end

Then in your registration view, you would use:

<!-- validation errors etc -->
<%= form_for @employee do |f| %>
  <!-- all your employee fields etc -->

  <%= f.fields_for :business do |b| %>
    <p>
      <%= b.label :name %>
      <br/>
      <%= b.text_field :name %>
    </p>

    <!-- more fields from business -->
  <% end %>
<% end %>

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!):

<!-- validation errors etc -->
<%= form_for @person do |f| %>
  <!-- all your person fields etc, assuming no extras for employee -->

  <% if @person.respond_to? :business %>
    <%= f.fields_for :business do |b| %>
      <p>
        <%= b.label :name %>
        <br/>
        <%= b.text_field :name %>
      </p>

      <!-- more fields from business -->
    <% end %>
  <% end %>
<% end %>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文