nill class 的未定义方法 'about'

Undefined method 'about' for nill class

我有两个table:User有一个AccountAccountbelongs_toUser。当我想从字段 'about' (table Accounts) 中获取一些值时,我遇到了 [=25= 的 'undefined method about' 问题]'。挑战是获取关注者列表并从另一个 table 获取他们的头像或信息 'about' 并将其输出到视图中。

我在控制器中的方法

 def list_of_follower
   @followers_id = Follow.select("follower_id ").where("followable_id = ?", current_user)
   @followers = User.where("id in (?)", @followers_id)
   @followables_id = Follow.select("followable_id").where("follower_id = ?", current_user)
   @followables = User.where("id in (?)", @followables_id)
end

查看list_of_follower.html.haml

%h1 My followers
 - @followers.each do |f|
  %ul.list-group
    %li.list-group-item
      %p=f.name
      %p=f.account.about
%h1 I'm follower
- @followables.each do |followable|
  %ul.list-group
%li.list-group-item
  %p=followable.name
  %p=followable.account.about

Create_Accounts.rb

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.belongs_to :user, index: true
      t.text :about
      t.timestamps null: false
    end
 end
end

User.rb

class User < ActiveRecord::Base
 acts_as_followable
 acts_as_follower
 acts_as_liker
 has_one :account
 has_many :posts
 has_many :comments

accepts_nested_attributes_for :account
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

def email_required?
  false
end

def email_changed?
  false
end

 validates :login, :email, uniqueness: true
end

Account.rb

class Account < ActiveRecord::Base
 belongs_to :user
  mount_uploader :avatar, AvatarUploader
 end

tableUser内容显示没有问题(工作请求),但是与之关联的table内容不显示,请问是什么问题?

我认为,问题不是每个用户都有一个帐户。 你可以试试这个:

%p=f.account.try(:about)