属性未显示在用户

属性未显示在用户

本文介绍了属性未显示在用户#show 页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

users_controller.rb

class ProfilesController 

user#show.html.erb

 

种族:<%= @[email protected] : "" %>

<div class="元素">教育:<span class="select_for_education"><%= @[email protected] : "" %>

user.education.name 仅显示以下内容 - 教育:

不显示使用以下选择在他的个人资料中选择的用户教育:

 

种族:<%= best_in_place current_user, :ethnicity_id, :type =>:select, 集合: Ethnicity.all.map{|e|[e.id, e.name]}, :inner_class =>'education-edit', nil: '选择种族' %>

<div class="元素">教育:<span class="select_for_education"><%= best_in_place current_user, :education_id, :type =>:选择,集合:Education.all.map{|e|[e.id, e.name]}, :inner_class =>'education-edit', nil: '选择教育'%>

我做错了什么?我怎样才能让用户在他/她自己的个人资料上显示的教育显示在显示页面中?

提前致谢!

User.rb

class User [:facebook, :twitter, :linkedin]attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,:生日, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,:photos_attributes, :age, :education_id, :ethnicity_id, :blurbhas_many :authorizations, :dependent =>:破坏has_many :评论has_many : 事件has_many :photos, as: :attachablehas_many :问题has_many :sent_messages, class_name: 'Message', foreign_key: :sender_idhas_many :received_messages, class_name: 'Message', foreign_key: :receiver_idhas_one : 种族has_one :教育结尾

ethnicity.rb

职业种族

education.rb

班级教育
解决方案

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

鉴于您的模型,虽然可以想象相反的方式,但我认为关联应该如下所示:

# 用户模型类用户

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

user#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>

The user.education.name is just showing the following - Education:

Without showing the users Education in which was chosen on his personal profile using the following select:

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

What am I doing wrong? And how can I get the users education that DOES display on his/her own profile to display in the show page?

Thanks in advanced!

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
解决方案

You're missing belongs_to association for has_many and has_one relation. The belongs_to association definition needs to be defined in the model whose table has the foreign key.

Given your models, although the other way around is imaginable, here is what I think the associations should look like:

# 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

这篇关于属性未显示在用户#show 页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:07