【发布时间】:2012-03-22 15:10:21
【问题描述】:
我有模型 User、Teacher、TeacherEducation。 TeacherEducation 属于 Teacher,Teacher 属于 User。我使用嵌套属性通过控制器中的一行保存所有内容。但是当我的应用程序显示验证错误时,我得到了非常奇怪的 TeacherEducation 字段名称。例如:
Teacher teacher education teacher education university can't be empty.
我没有Teacher teacher education teacher education university 名称,我在 locales/en.yml
en:
activerecord:
attributes:
...
teacher_education:
teacher_education_university: "Univer"
而且我在 User 或 Teacher 模型中没有得到这么奇怪的名字。那么,我怎样才能显示正确的消息?
我的模型:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :user_login,
:password,
:teacher_attributes
has_one :teacher
accepts_nested_attributes_for :teacher
end
class Teacher < ActiveRecord::Base
attr_accessible :teacher_last_name,
:teacher_first_name,
:teacher_middle_name,
:teacher_birthday,
:teacher_sex,
:teacher_category,
:teacher_education_attributes
belongs_to :user
has_one :teacher_education
accepts_nested_attributes_for :teacher_education
end
class TeacherEducation < ActiveRecord::Base
attr_accessible :teacher_education_university,
:teacher_education_year,
:teacher_education_graduation,
:teacher_education_speciality
belongs_to :teacher
validates :teacher_education_university,
:presence => { :message => "can't be empty" }
end
我的控制器:
class AdminsController < ApplicationController
def create_teacher
user = User.new( params[:user] )
user.user_role = "teacher"
if user.save
...
else
all_err = user.errors.full_messages
...
user_errors = all_err.to_sentence :last_word_connector => ", ",
:two_words_connector => ", "
flash[:error] = user_errors if user_errors.present?
end
end
我的哈希:
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
password: ''
teacher_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
teacher_birthday: ''
teacher_category: ''
teacher_education_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
teacher_education_graduation: ''
teacher_education_speciality: ''
teacher_education_university: ''
teacher_education_year: ''
teacher_first_name: ''
teacher_last_name: ''
teacher_middle_name: ''
teacher_sex: w
user_login: ''
【问题讨论】:
标签: ruby-on-rails ruby model