All threads / Devise User with separate Profile

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Devise User with separate Profile

Benny Maas asked in Gems / Libraries

Hi,
i am using Devise and i want to a separate profile for each user with additional informations like address, phone, mobile, ...

After Signup, the user should be forced to fill out his profile.

right now i have the standard devise user model and the profile model.

# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string           default(""), not null
#  encrypted_password     :string           default(""), not null
#  reset_password_token   :string
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0), not null
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :inet
#  last_sign_in_ip        :inet
#  confirmation_token     :string
#  confirmed_at           :datetime
#  confirmation_sent_at   :datetime
#  unconfirmed_email      :string
#  failed_attempts        :integer          default(0), not null
#  unlock_token           :string
#  locked_at              :datetime
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#

class User < ActiveRecord::Base

  has_one :profile, dependent: :destroy
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end
# == Schema Information
#
# Table name: profiles
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  title      :string
#  first_name :string
#  last_name  :string
#  street     :string
#  zip        :string
#  city       :string
#  country    :string
#  phone      :string
#  mobile     :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Profile < ActiveRecord::Base
  belongs_to :user

  validates :title, :first_name, :last_name, :country, :mobile, presence: true
end

Any hint how i can start?

Hey Benny,

You can redirect after sign up the a ProfilesController new action. If you want to enforce it, you can set a before_action :require_profile! that redirects users to that until they have created one. All sorta depends on how you want to the user experience to be like. Just make sure your require_profile before_action gets skipped on the new and create methods for the ProfilesController and that should do the trick!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.