3

我有一个Rails应用程序。我安装了Devise。我想在用户模型和不同的配置文件之间有一个多态关联,例如:Student和Mentor。为不同的用户配置文件设计多态关联的用户

这就是我想要做的:有一个学生注册页面,例如:emailpassword,password_confirmation。但是,我也希望有特定的学生档案领域,如residency_typegraduation_year

user.rb 
class User < ActiveRecord::Base 
    belongs_to :profile, polymorphic: true 

student_profile.rb 
class StudentProfile < ActiveRecord::Base 
    has_one :user, as: :profile, dependent: :destroy 

registrations_controller.rb 
class RegistrationsController < Devise::RegistrationsController 
    def new_student_registration 
    resource = build_resource({}) 
    respond_with resource 
    end 
... 

我可以创建一个控制台,像这样的学生配置文件的用户...

u = User.new 
u.email = "[email protected]" 
... (password and password confirmation) 
u.profile = StudentProfile.new 
u.graduation_year = "2014" 
u.save 

问题是,我无法弄清楚如何设置控制器和一个窗体来做到这一点。我该怎么做?请注意用户和学生档案之间的关系。

+2

你解决了这个问题吗?你介意发回你的解决方案吗? – brg 2013-04-24 03:03:43

回答

0

关注此导轨投插曲http://railscasts.com/episodes/210-customizing-devise

因为我认为你只需要在新的页面形式和用户模型,使其接受嵌套属性添加字段的轮廓。要知道什么是配置文件,你可以有一个选择框和之前的过滤器,以查看基于该值的配置文件