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

因爲我認爲你只需要在新的頁面形式和用戶模型,使其接受嵌套屬性添加字段的輪廓。要知道什麼是配置文件,你可以有一個選擇框和之前的過濾器,以查看基於該值的配置文件