Ruby on rails 如何在布局中显示变量?

Ruby on rails 如何在布局中显示变量?,ruby-on-rails,devise,Ruby On Rails,Devise,我的模型用户与关系: belongs_to :freelancer 和自由职业者的模特,包括: belongs_to :user. 用于注册使用设计。 注册后,将自动创建一个新用户,其用户id=当前用户.id。注册或登录并传入布局模板后,如何更好地使用user\u id=current user获取@freeloper对象: layouts/\u user\u panel.html.erb <div class="user-panel_head"> <%= lin

我的模型用户与关系:

belongs_to :freelancer
和自由职业者的模特
,包括:

belongs_to :user. 
用于注册使用设计。 注册后,将自动创建一个新用户,其用户id=当前用户.id。注册或登录并传入布局模板后,如何更好地使用
user\u id=current user
获取
@freeloper
对象:

layouts/\u user\u 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
#==架构信息
#
#表名:用户
#
#id:整数不为空,主键
#电子邮件:字符串默认值(“”),不为空
#加密密码:字符串默认值(“”),不为空
#重置密码\u令牌:字符串
#重置密码发送时间:datetime
#还记得在:datetime创建的吗
#计数中的符号:整数默认值(0),不为空
#当前登录时间:datetime
#上次登录时间:datetime
#ip中的当前符号:字符串
#ip中的最后一个签名:字符串
#创建时间:datetime非空
#更新时间:datetime非空
#确认令牌:字符串
#确认时间:datetime
#确认发送时间:datetime
#未确认的电子邮件:string
#失败的\u尝试:整数默认值(0),非空
#解锁令牌:字符串
#锁定时间:datetime
#用户名:string
#
#索引
#
#索引用户在确认令牌(确认令牌)上唯一
#索引用户在电子邮件上(电子邮件)唯一
#索引用户在重置密码令牌(重置密码令牌)上唯一
#索引\u用户\u在\u解锁\u令牌(解锁\u令牌)上唯一
#
类用户<应用程序记录
#包括默认设计模块。其他可供选择的项目包括:
#:可确认、:可锁定、:超时和:全授权
属于:自由职业者
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证,:可确认,:可锁定
结束
自由职业者模式

# == 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']
#==架构信息
#
#表名:自由职业者
#
#id:整数不为空,主键
#名字:string
#姓氏:string
#比率:整数
#生日:日期
#位置:字符串
#说明:文本
#地点:字符串
#可见:布尔值
#阿凡达:字符串
#类别id:整数
#创建时间:datetime非空
#更新时间:datetime非空
#用户id:integer
#专业化:字符串
#价格类别:字符串
#所有权:字符串
#
#索引
#
#分类id(分类id)上的自由职业者索引
#用户id(用户id)上的自由职业者索引
#
职业自由职业者<应用记录
属于:类别
属于:用户
有很多:链接
_和_属于_很多:支付选项
接受链接的嵌套属性,允许销毁:true
价格类别=[“项目”、“小时”、“月”、“1000个字符”]
所有权类型=[“个人”、“公司”]

在本例中,
当前用户
@自由职业者
之间有什么区别?您正在使用来自
当前用户的数据填充视图,这意味着它们是同一个对象,所以您不能将
链接到当前用户,

无论如何,为了解决您的问题——控制器中的实例变量被传递到布局,就像它们传递到模板和部分一样。他们都被认为是观点,通常表现出相似的方式。因此,您需要在负责呈现页面的任何操作中,在控制器中设置
@freegorer

在设计的情况下,您应该考虑重写 SurvivsUpthPosith< < /C> >方法,并返回您希望用户在注册后重定向的路由。

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
类用户::注册控制器<设计::注册控制器
#在这种情况下,Resource是一个用户
注册后的def路径(资源)
超级(资源)
用户路径(资源)#返回“用户”显示路径的路径
结束
结束
结束

因此,您需要在与
用户路径
关联的控制器操作中分配
@freegorer
。同样的原则也适用于Deviate的SessionController。试试这个,它会在注册后立即创建自由职业者

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
class用户
如果您遇到问题,现在刚刚修复的应该可以工作了。这一点也很好,用户和自由职业者的ID将始终相同。