Ich habe Modell Benutzer mit Beziehung:Wie macht man eine Variable im Layout sichtbar?
belongs_to :freelancer
und Modell Freelancer mit:
belongs_to :user.
Für die Registrierung verwenden Devise. Nach der Registrierung wird automatisch ein neuer Benutzer Freelancer-Objekt mit user_id = current user.id
erstellt. Wie besser Weg ich @freelancer
Objekt mit user_id = current user
nach anmelden oder Zeichen in und in Layout-Vorlage übergeben:
Layouts/_user_panel.html.erb
<div class="user-panel_head">
<%= link_to @freelancer, title: current_user.username, class: "user-panel__avatar active" do %>
<%= image_tag "default/avatar.png", class: "avatario" %>
<% end %>
<div class="user-panel__side">
<%= link_to current_user.username, @freelancer, class: "user-panel__user-name" %>
<span class="btn_drop icon_arrow_up" role="expand_menu_trigger"></span>
</div>
</div>
UPDATE:
# == 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 :string
# last_sign_in_ip :string
# created_at :datetime not null
# updated_at :datetime not null
# 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
# username :string
#
# Indexes
#
# index_users_on_confirmation_token (confirmation_token) UNIQUE
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_unlock_token (unlock_token) UNIQUE
#
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
belongs_to :freelancer
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
end
FREELANCER MODELL
# == Schema Information
#
# Table name: freelancers
#
# id :integer not null, primary key
# first_name :string
# last_name :string
# rate :integer
# birthday :date
# location :string
# description :text
# site :string
# visible :boolean
# avatar :string
# category_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer
# specialization :string
# price_category :string
# ownership :string
#
# Indexes
#
# index_freelancers_on_category_id (category_id)
# index_freelancers_on_user_id (user_id)
#
class Freelancer < ApplicationRecord
belongs_to :category
belongs_to :user
has_many :links
has_and_belongs_to_many :payment_options
accepts_nested_attributes_for :links, allow_destroy: true
PRICE_CATEGORIES = ['Project', 'Hour', 'Month', 'For 1000 characters']
OWNERSHIP_TYPES = ['Individual', 'Company']
Wenn Sie Probleme damit hatten, sollte gerade jetzt behoben werden. Auch ein guter Punkt dabei, User und Freelancer IDs werden immer gleich sein. – 7urkm3n