我的模型用户有关系:
belongs_to :freelancer
和模型自由职业者:
belongs_to :user.
注册时使用设计。
注册后,将使用user_id = current user.id
自动创建具有Freelancer对象的新用户。在注册或登录并传入布局模板后,如何更好地获取@freelancer
user_id = current user
对象:
布局/ _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>
更新:
# == 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模型
# == 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']
答案 0 :(得分:1)
此示例中current_user
和@freelancer
之间的区别是什么?您使用current_user
中的数据来填充您的观点,这意味着他们是同一个对象,所以您不能link_to current_user, ...
吗?
无论如何,为了解决你的问题 - 控制器中的实例变量会像模板和部分一样传递给布局。他们都被认为是观点,并且通常以类似的方式表现。因此,您需要在负责呈现页面的任何操作中在控制器中设置@freelancer
。
对于Devise,您应该考虑覆盖after_sign_up_path_for
方法并返回您希望用户在注册后重定向到的路由。
class Users::RegistrationsController < Devise::RegistrationsController
# Resource is a User in this case
def after_sign_up_path_for(resource)
super(resource)
user_path(resource) # Return the path for the `users#show` route
end
end
end
因此,您要在与@freelancer
相关联的控制器操作中指定user_path
。同样的原则适用于Devise的SessionsController
答案 1 :(得分:1)
试试这个,它会在Freelancer
后立即创建sign_up
。
class User < ActiveRecord::Base
has_one :freelancer, dependent: :destroy
before_create :set_freelancer
def set_freelancer
build_freelancer(id: self.id, user_id: self.id, email: self.email)
end
end
class Freelancer < ActiveRecord::Base
belongs_to :user
end