当有多个模型时,Rails 设计 STI 或多态?
我有一个问题。我无法弄清楚如何使用设计解决此身份验证问题,并且我不知道是否应该在模型或 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会选择 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.