未在用户显示的属性#show page

users_controller.rb

class ProfilesController < ApplicationController before_filter :authenticate_user! def show @user = User.find(params[:id]) || User.find(current_user.id) @questions_for_about = @user.questions.for_about.order('id asc') @questions_for_personality = @user.questions.for_personality.order('id asc') end end 

用户#show.html.erb

  
Ethinicity:
Education:

user.education.name只显示以下内容 – 教育:

未显示用户使用以下选项在其个人资料中选择的教育:

  
Ethinicity: :select, collection: Ethnicity.all.map{|e| [e.id, e.name]}, :inner_class => 'education-edit', nil: 'Select Ethnicity' %>
Education: :select, collection: Education.all.map{|e| [e.id, e.name]}, :inner_class => 'education-edit', nil: 'Select Education' %>

我究竟做错了什么? 如何让用户在他/她自己的个人资料上显示的教育显示在显示页面中?

提前致谢!

User.rb

 class User  [: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 

ethnicity.rb

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

education.rb

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

你缺少has_manyhas_one关系的belongs_to关联。 belongs_to关联定义需要在其表具有外键的模型中定义。

鉴于你的模型,虽然相反的方式是可以想象的,我认为这些关联应该是这样的:

 # User Model class User < ActiveRecord::Base ... belongs_to :ethnicity belongs_to :education end # Ethnicity Model class Ethinicity < ActiveRecord::Base attr_accessible :name has_many :users end # Education Model class Education < ActiveRecord::Base attr_accessible :name has_many :users end