Ruby on rails 未定义的方法`name';对于nil:NilClass 3模型

Ruby on rails 未定义的方法`name';对于nil:NilClass 3模型,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,所以,我有3个模型 具有种族标识和教育标识关联的用户模型 一种具有用户id关联的教育模型 一种具有用户身份关联的种族模型 现在它的工作原理是,例如,用户可以通过选择已作为种子数据随教育迁移而迁移的各种教育的下拉列表来添加教育。因此,如果用户选择了单身汉,这就变成了users education.name,如下所示: <span class="select_for_education"> <%= best_in_place current_user, :edu

所以,我有3个模型

具有种族标识和教育标识关联的用户模型

一种具有用户id关联的教育模型

一种具有用户身份关联的种族模型

现在它的工作原理是,例如,用户可以通过选择已作为种子数据随教育迁移而迁移的各种教育的下拉列表来添加教育。因此,如果用户选择了单身汉,这就变成了users education.name,如下所示:

  <span class="select_for_education">
        <%= best_in_place current_user, :education_id, :type => :select, collection: Education.all.map{|e| [e.id, e.name]}, :inner_class => 'education-edit', nil: 'Select Education' %>
        </div>

:select,collection:Education.all.map{| e |[e.id,e.name]},:internal_class=>'Education edit',nil:'select Education'>
现在,用户可以选择一个教育np,并将其保存到其个人配置文件中,但在显示用户的education.name时,出现以下错误:

错误消息:

 undefined method `name' for nil:NilClass


       <div class="element">
         Ethinicity:
         <%= @user.ethnicity.name.present? ? @user.ethnicity.name : "" %>
       </div>
nil:NilClass的未定义方法“name” 乙炔度: 请看下面的模型

种族。rb

class Ethnicity < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users

end
class Education < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users


end
class User < ActiveRecord::Base
  include PgSearch

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         :omniauth_providers => [:facebook, :twitter, :linkedin]

  attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,
                  :birthday, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,
                  :photos_attributes, :age, :education_id, :ethnicity_id, :blurb

  has_many :authorizations, :dependent => :destroy
  has_many :comments
  has_many :events
  has_many :photos, as: :attachable
  has_many :questions
  has_many :sent_messages, class_name: 'Message', foreign_key: :sender_id
  has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
  has_one  :ethnicity
  has_one  :education
end
<div class="element">
          Ethinicity:
          <%= @user.ethnicity.name.present? ? @user.ethnicity.name : "" %>
        </div>
        <div class="element">
          Education:
          <span class="select_for_education">
           <%= @user.education.name.present? ? @user.education.name : "" %>
        </div>
class
education.rb

class Ethnicity < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users

end
class Education < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users


end
class User < ActiveRecord::Base
  include PgSearch

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         :omniauth_providers => [:facebook, :twitter, :linkedin]

  attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,
                  :birthday, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,
                  :photos_attributes, :age, :education_id, :ethnicity_id, :blurb

  has_many :authorizations, :dependent => :destroy
  has_many :comments
  has_many :events
  has_many :photos, as: :attachable
  has_many :questions
  has_many :sent_messages, class_name: 'Message', foreign_key: :sender_id
  has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
  has_one  :ethnicity
  has_one  :education
end
<div class="element">
          Ethinicity:
          <%= @user.ethnicity.name.present? ? @user.ethnicity.name : "" %>
        </div>
        <div class="element">
          Education:
          <span class="select_for_education">
           <%= @user.education.name.present? ? @user.education.name : "" %>
        </div>
课堂教学
user.rb

class Ethnicity < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users

end
class Education < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users


end
class User < ActiveRecord::Base
  include PgSearch

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         :omniauth_providers => [:facebook, :twitter, :linkedin]

  attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,
                  :birthday, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,
                  :photos_attributes, :age, :education_id, :ethnicity_id, :blurb

  has_many :authorizations, :dependent => :destroy
  has_many :comments
  has_many :events
  has_many :photos, as: :attachable
  has_many :questions
  has_many :sent_messages, class_name: 'Message', foreign_key: :sender_id
  has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
  has_one  :ethnicity
  has_one  :education
end
<div class="element">
          Ethinicity:
          <%= @user.ethnicity.name.present? ? @user.ethnicity.name : "" %>
        </div>
        <div class="element">
          Education:
          <span class="select_for_education">
           <%= @user.education.name.present? ? @user.education.name : "" %>
        </div>
class用户[:facebook,:twitter,:linkedin]
属性可访问:电子邮件、:密码、:密码确认、:zip、:性别、:记住我、:名字、:姓氏、,
:生日,:当前密码,:职业,:地址,:兴趣,:关于我,:个人资料\u图像,
:照片\u属性,:年龄,:教育\u id,:种族\u id,:布告
拥有多个:授权,:依赖=>:销毁
有很多评论
你参加过很多活动吗
有很多:照片,如::可附加
有很多问题吗
有多条:已发送消息,类名称:“消息”,外键::发送者id
已接收多条消息,类名称:'Message',外键::接收方id
有一个:种族
有一个:教育
结束
下面是我目前试图在show页面中向其他用户显示用户教育的方式

show.html.erb

class Ethnicity < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users

end
class Education < ActiveRecord::Base
  attr_accessible :name
  has_many_and_belongs_to_many :users


end
class User < ActiveRecord::Base
  include PgSearch

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable,
         :omniauth_providers => [:facebook, :twitter, :linkedin]

  attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,
                  :birthday, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,
                  :photos_attributes, :age, :education_id, :ethnicity_id, :blurb

  has_many :authorizations, :dependent => :destroy
  has_many :comments
  has_many :events
  has_many :photos, as: :attachable
  has_many :questions
  has_many :sent_messages, class_name: 'Message', foreign_key: :sender_id
  has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
  has_one  :ethnicity
  has_one  :education
end
<div class="element">
          Ethinicity:
          <%= @user.ethnicity.name.present? ? @user.ethnicity.name : "" %>
        </div>
        <div class="element">
          Education:
          <span class="select_for_education">
           <%= @user.education.name.present? ? @user.education.name : "" %>
        </div>

乙炔度:
教育:
在所有情况下,更换

@user.identiality.name.present?
@user.identiality.present?

@user.education.name.present?
一起使用
@user.education.present?


另一方面,
具有_且_属于_many
是一种对称关系。也就是说,如果种族有很多用户,那么用户就有很多种族。我认为情况并非如此。相反,我认为你可能想要

class Ethnicity < ActiveRecord::Base
  attr_accessible :name
  has_many :users
end
class

课堂教学

这不是你问题中出现错误的原因,但我认为这可能有用。

谢谢,马特,我已经这么做了,但是教育还没有显现出来?现在它只显示了以下内容-Education(空白,当我知道Education实际上是保存到用户自己的配置文件中,而不是显示给其他人时)您没有显示任何将用户与种族关联的代码,因此我恐怕无法帮到您。现在这个错误已经修复,你可以问一个新问题,其中包括你的数据。用户模型也在上面的初始帖子中提供,包括种族关联。是的,我觉得很好。如何将用户和种族数据插入数据库,以及如何检索这些数据?此代码通常位于控制器中。这是我们需要看到的,也是一个恰当的问题,因为当前标题“nil:NilClass 3模型的未定义方法`名称'”中的问题已经解决。公平地说,我目前没有任何教育或种族控制者,因为这些都不是必需的。也许您可以告诉我一行代码,添加到user controller show action以显示user.education.name?提前谢谢。