Ruby on rails 属性未显示在用户#显示页面上
用户\u控制器.rbRuby on rails 属性未显示在用户#显示页面上,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,用户\u控制器.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') @
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
<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>
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
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class ProfilesController
user#show.html.erb
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
<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>
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
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end
乙炔度:
教育:
user.education.name仅显示以下内容-教育:
不显示用户在其个人资料中使用以下选择选择的教育:
<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>
乙炔度:
:select,collection:ocidentity.all.map{| e |[e.id,e.name]},:internal_class=>'education edit',nil:'select ocidentity'>
教育:
:select,collection:Education.all.map{| e |[e.id,e.name]},:internal_class=>'Education edit',nil:'select Education'>
我做错了什么?我如何才能让显示在他/她自己的个人资料中的用户教育显示在显示页面中
提前感谢
User.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
<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>
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
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class用户[:facebook,:twitter,:linkedin]
属性可访问:电子邮件、:密码、:密码确认、:zip、:性别、:记住我、:名字、:姓氏、,
:生日,:当前密码,:职业,:地址,:兴趣,:关于我,:个人资料\u图像,
:照片\u属性,:年龄,:教育\u id,:种族\u id,:布告
拥有多个:授权,:依赖=>:销毁
有很多评论
你参加过很多活动吗
有很多:照片,如::可附加
有很多问题吗
有多条:已发送消息,类名称:“消息”,外键::发送者id
已接收多条消息,类名称:'Message',外键::接收方id
有一个:种族
有一个:教育
结束
种族。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
<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>
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
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class Ethinicity
education.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
<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>
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
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end
课堂教学
您缺少的属于关联,因为有多个和有一个关系。属于_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
#用户模型
类用户
如何将用户
、种族
和教育
联系起来?你能发布你的模型吗?当然,请查看更新的帖子!