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!

I see this kind of question (setting up a Profile at User sign-up) all over the internet. Is there a starter or boilerplate or a well-written complete guide/tutorial somewhere on GitHub or the web? (Most people learning web development want to learn this feature pretty early in their journey, so it would be great if there are education-materials for it).

Join the discussion

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

Join 68,099+ 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.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.