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?

Reply

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!

Reply

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).

Reply

After lots of searching this seems pretty simple, for example,
say I need three sign ins , one for admin, one for suppliers and one for members.

To create a login for say supplier run
rails generate devise supplier
This does three things.

  1. Adds routes
  2. Create a Supplier model or add the devise modules to an existing model
  3. Creates a migration to add the appropriate columns and indexes to your model.

Note: If you have an existing model that you want to us devise on, simple run the generator, it will do the above taking into account any existing columns such as email that may already be present in your model.

Reply
Join the discussion
Create an account Log in

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

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

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