我想在遵循宪法时在用户保存时生成'个人资料'数据
如何做某事?
用户表数据
+-------+-------------------+----------+-----+--------------------+
| id | email | nickname | sex | encrypted_password |
+-------+-------------------+----------+-----+--------------------+
| 95425 | example@gmail.com | Citrus | 1 | |
+-------+-------------------+----------+-----+--------------------+
个人资料表数据
+---------+------------+-------------+
| user_id | birth_year | birth_month |
+---------+------------+-------------+
| 95425 | 1982 | 12 |
+---------+------------+-------------+
signup.html.slim
= f.label :sex, 'Mens', :value => 0
= f.radio_button :sex, true, :checked => true
= f.label :sex, 'Women', :value => 1
= f.radio_button :sex, false
= f.text_field :email
= f.password_field :password_confirmation
= fields_for :profile do |c|
= c.date_select(:birth_year, use_month_numbers: true,start_year:1930, end_year: Time.now.year,date_separator: '/') %>
答案 0 :(得分:0)
很高兴拥有user profile
,但我会建议在注册后执行此操作,因为我相信只需使用用户email/username
和{注册就应该尽可能简单{1}}和date of birth
..就是这样。我已经实现了相同但我为使用观察者的每个用户创建默认配置文件一旦使用devise保存用户。这就是我所做的。您可以与password
模型建立profile
关联,user
与profile_pictures
profile
的每个belongs to
关联。这就是我所做的: -
user
signup
表单
email,password,date of birth and current location
中使用此数据创建observer
,如下所示my user.rb
default profile
my profile.rb
has_one :profile,:dependent => :destroy
我的user_observer.rb
belongs_to :user
has_many :profile_pictures, :class_name => 'ProfilePicture', :dependent => :destroy
希望这有助于......