2013-12-21 52 views
1

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

<div class="element"> 
      Ethinicity: 
      <%= @user.ethnicity.present? ? @user.ethnicity.name : "" %> 
     </div> 
     <div class="element"> 
      Education: 
      <span class="select_for_education"> 
      <%= @user.education.present? ? @user.education.name : "" %> 
     </div> 

的user.education.name只是呈現以下 - 教育:

沒有顯示用戶在他的個人資料中使用fo選擇:

<div class="element"> 
      Ethinicity: 
      <%= best_in_place current_user, :ethnicity_id, :type => :select, collection: Ethnicity.all.map{|e| [e.id, e.name]}, :inner_class => 'education-edit', nil: 'Select Ethnicity' %> 
     </div> 
     <div class="element"> 
      Education: 
      <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> 

我在做什麼錯?我怎樣才能讓他/她自己的個人資料上顯示的用戶教育能夠在顯示頁面中顯示?

感謝先進!

User.rb

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 

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 
+0

如何關聯的是'user','ethnicity'和'education'?你可以發佈你的模型? – vee

+0

當然,請看更新的帖子! –

回答

2

你想念g belongs_to關聯has_manyhas_one關係。關聯定義需要在表中有外鍵的模型中定義。

鑑於你的機型,雖然周圍的另一種方法是想象,這裏是我認爲協會應該是這樣的:

# 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