ruby on rails - I want to use Devise a user table with association another table -
ruby on rails - I want to use Devise a user table with association another table -
i want generate 'profile' info @ time of user preservation @ time of next constitution
how something?
users table data
+-------+-------------------+----------+-----+--------------------+ | id | email | nickname | sex | encrypted_password | +-------+-------------------+----------+-----+--------------------+ | 95425 | example@gmail.com | citrus | 1 | | +-------+-------------------+----------+-----+--------------------+
profiles table data
+---------+------------+-------------+ | 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 |c| = c.date_select(:birth_year, use_month_numbers: true,start_year:1930, end_year: time.now.year,date_separator: '/') %>
its have user profile
recommend after signup believe signup should simple possible using users email/username
, date of birth
, password
..thats it.i have implemented same create default profile every user using observer 1 time user saved using devise.this did.you can have profile
association user
model , profile_pictures
associated every profile
belongs to
user
.this did:-
signup
form using email,password,date of birth , current location
then utilize info in observer
create default profile
shown below my user.rb
has_one :profile,:dependent => :destroy
my profile.rb
belongs_to :user has_many :profile_pictures, :class_name => 'profilepicture', :dependent => :destroy
my user_observer.rb
class userobserver < activerecord::observer ##this method run after every user created , create new default profile def after_create(user) rails.logger.info "creating default profile pic #{user.email}==================" ##first username email id removing @ , other fields if user.provider.blank? name = user.email[/[^@]+/] name=name.split(".").map {|n| n.capitalize }.join(" ") user.username = name end ##here create new profile username user.create_profile(:title=>user.username) default_profile_picture=user.profile.profile_pictures.new({ :avatar => file.new("#{rails.root}/app/assets/images/dashboard/default_avatar.jpg"), :active => true }) default_profile_picture.save ##save user default profile(with default profile picture) user.save! ##send welcome mail service new user using sidekiq redis usermailer.delay.registration_confirmation(user) end end
hope helps....
ruby-on-rails devise
Comments
Post a Comment