我有一個Rails應用程序。我安裝了Devise。我想在用戶模型和不同的配置文件之間有一個多態關聯,例如:Student和Mentor。爲不同的用戶配置文件設計多態關聯的用戶
這就是我想要做的:有一個學生註冊頁面,例如:email
,password
,password_confirmation
。但是,我也希望有特定的學生檔案領域,如residency_type
和graduation_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
問題是,我無法弄清楚如何設置控制器和一個窗體來做到這一點。我該怎麼做?請注意用戶和學生檔案之間的關係。
你解決了這個問題嗎?你介意發回你的解決方案嗎? – brg 2013-04-24 03:03:43