The sample is 1 to 1
Use devise
for Auth
The contents of the table are as in the image below
Describe has_one
and belongs_to
in each
In the case of one-to-many, it becomes has_many
.
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile
end
profile.rb
class Profile < ApplicationRecord
belongs_to :user
end
user_id uses current_user.id to perform
assignment processing
profiles_controller.rb
def create
@profile = Profile.new(profile_params)
#Data sent from the form(user_Excluding id)Is assigned via the strong parameter
@profile.user_id = current_user.id
# user_id is current_user.Assign value using id
@profile.save
redirect_to profiles_path
end
The method chain can be used by making the association in the model settings.
***.rb
user.profile
profile.user
called ʻuser instead of the devise controller,
profile is handled by user_controller` using the method chain.$ rails g controller users
users_controller.rb
def show
@user = User.find(params[:id])
@profile = @user.profile
#You can retrieve the profile value from user.
end
Recommended Posts