RoR变量不包含相关记录

时间:2016-08-10 15:31:37

标签: ruby-on-rails-4

RoR新手,这里找不到我想要的解决方案,所以我在这里输入我的问题:

我有一个网络应用程序,在用户登录后,他们将被重定向到仪表板,向他们显示各种信息。

现在 - 正如我对RoR的感觉一样 - 仪表板的视图可以提供大量信息:

 def index
    @logged_user = Person.includes(:user, :addresses).find(session[:user_id])
 end

但是当我使用@ logged_user.inspect进行检查时,它只给了我这个

   #<Person (person model attributes here) >

我检查了日志,似乎已加载用户和地址

printscreen of my log http://prntscr.com/c40fee

但它们不在@logged_user变量中...我在这里错过了一步吗?我哪里出错了?

编辑:

这是Person的模型

 class Person < ActiveRecord::Base
   validates :first_name, :last_name, presence: true

   has_one :user, :dependent => :destroy 
   has_one :medical_record
   has_many :addresses, :dependent => :destroy
   has_many :contacts,:dependent => :destroy
   has_many :enrollments_as_student, :class_name => 'Enrollment', :foreign_key => :student_id, :dependent => :destroy

   accepts_nested_attributes_for :addresses, allow_destroy: true
   accepts_nested_attributes_for :contacts, allow_destroy: true
   accepts_nested_attributes_for :medical_record, allow_destroy: true
 end

用户模型

class User < ActiveRecord::Base
  validates :username, presence: true
  validates :username, :password, format: {with: /\A[a-zA-Z0-9 ñÑ]+\Z/, message: "Special characters aren't allowed"}
  validates :username, uniqueness: {message: "username is already taken"}

  has_secure_password

  belongs_to :person

地址模型

class Address < ActiveRecord::Base
  validates :street, :barangay, :city, :province,
    presence: {message: "Fill in all necessary fields"}
  validates :zipcode,
    numericality: {message: "Zipcodes should only contain numbers"},
    length: {minimum: 4, maximum: 4, message: "Invalid zipcode length"}

  belongs_to :person
end

也是登录的片段

def login

  @user = User.find_by_username(params[:receptions][:username])

  if @user && @user.authenticate(params[:receptions][:password])
      session[:user_id] = @user.person_id
      flash[:notice] = "You have succfully logged in"
      redirect_to '/dashboards'
    else
      flash[:notice] = "Invalid username or password"
      redirect_to '/login'
end

0 个答案:

没有答案