当有多个模型时,Rails 设计 STI 或多态?

发布于 2024-10-09 21:52:47 字数 1050 浏览 0 评论 0原文

我有一个问题。我无法弄清楚如何使用设计解决此身份验证问题,并且我不知道是否应该在模型或 STI 中使用多态关联。

我有 2 个模型,例如教师和学生。我正在尝试建立一个多态关联。

这是我的模型:

   Teacher model:
        class Teacher < ActionController::Base
        has_one :user, :as => :profileable 
        devise :database_authenticatable, :registerable 
        end
    Student model:
        class Student < ActionController::Base
        has_one :user, :as => :profileable 
        devise :database_authenticatable, :registerable 
        end
    User model:
        class User < ActionController::Base
        belongs_to :profileable, :polymorphic => true
         attr_accessible :email, :password, :password_confirmation, :remember_me
        end

我的routes.rb

School::Application.routes.draw do
  devise_for :users
  devise_for :teachers
  devise_for :students

我已经为教师和学生创建了视图文件。 但我不知道如何拥有 1 个用户表和不同的视图和注册页面。

我想要 2 个注册页面。一份给老师,一份给学生。我希望用户表存储登录信息(电子邮件、密码...)。

我如何用 devise 创建这样的东西?

最好的问候,

Rails 初学者

I have a problem. I cant figure out how to solve this authentication problem with devise and i dont know if i should use a polymorphic association in the models or STI.

I have 2 models like Teacher and Student. And i am trying to make a polymorphic association.

Here is my models:

   Teacher model:
        class Teacher < ActionController::Base
        has_one :user, :as => :profileable 
        devise :database_authenticatable, :registerable 
        end
    Student model:
        class Student < ActionController::Base
        has_one :user, :as => :profileable 
        devise :database_authenticatable, :registerable 
        end
    User model:
        class User < ActionController::Base
        belongs_to :profileable, :polymorphic => true
         attr_accessible :email, :password, :password_confirmation, :remember_me
        end

My routes.rb

School::Application.routes.draw do
  devise_for :users
  devise_for :teachers
  devise_for :students

I have created the views files for both Teachers and Students.
But i cant figure out how to have 1 user table and differnts views and sign_up pages.

I want to have 2 sign up pages. One for Teacher and one for Student. I want the User table to store the login information(email, password ...).

How do I create such a thing with devise?

Best regards,

Rails beginner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

末蓝 2024-10-16 21:52:47

我会选择 STI 并使用存储用户首选项等的配置文件模型。至于每个模型的不同视图,请查看范围视图部分。

I would go for STI and use a Profile model storing user preferencies etc. As for different views per model take a look at the scoped views section.

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