3
我想重寫Devise的RegistrationsContollers
'創建操作,這樣當用戶註冊時,我可以將UserProfile
模型與該用戶相關聯。如何在用戶註冊時向用戶添加UserProfile? (Devise,Rails 3)
因此,以下的設計自述文件中的指導方針,我重寫動作:
#File app/controllers/registrations_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
def create
# some code here..
self.user_profiles.build #Error (no method `user_profiles`)
current_user.user_profiles.build #Error (current_user is nil)
some other way???
end
end
#File routes.rb:
devise_for :users, :controllers => { :registrations => 'users/registrations' }
設計是建立在users
表中的記錄,但我怎麼關聯與記錄中的UserProfile
?
我試過谷歌搜索,但我根本無法得到這個工作!任何幫助深表感謝。
(我現在用的設計1.1.5 on Rails的3.0.3)
解決:
,然後將溶液爲他人的利益:
#File app/controllers/registrations_controller.rb:
class Users::RegistrationsController < Devise::RegistrationsController
def create
super
@user.build_user_profile
@user.user_profile.some_data = 'abcd'
@user.save!
end
end
謝謝!該工程(@ user.build_user_profile)。另外,我需要在控制器中創建配置文件(不在模型中),因爲在註冊時(如地址)需要來自用戶的一些輸入。 – Zabba 2010-11-29 19:24:39