2012-03-22 10 views
0

の私のメッセージ私はモデルユーザー教師TeacherEducationを持っています。 教師教育は、に属します。教師は、ユーザーに属します。私はネストされた属性を使用して、コントローラ内のすべての行を1行で保存します。しかし、私はTeacherEducationのフィールドの非常に奇妙な名前を私のアプリケーションが検証エラーを示すときに取得します。たとえば:Railsの3 - 代わりnested_attributesモデルの奇妙な検証メッセージ

Teacher teacher education teacher education university can't be empty. 

私はTeacher teacher education teacher education university名前を持っていない、私はロケール/ en.yml

en: 
    activerecord: 
     attributes: 
     ... 
      teacher_education: 
      teacher_education_university: "Univer" 

に自分のエイリアスを設定し、私はユーザーに、このような奇妙な名前を得ることはありませんまたは教師モデル。では、どうすれば正しいメッセージを表示できますか?

マイモデル:

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

答えて

1

は、エラーメッセージが教師のTeacherEducation番号のteacher_education_university属性は空白であることを語っています。

ユーザーを保存しようとすると、@user.teacher.teacher_educationのteacher_education_university属性のエラーメッセージが返されます。

を使用しているため、Teacherが有効でない場合は、@userレコードを保存できません。 TeacherEducationが有効でない場合、Teacherを保存することはできません。

+0

私はそれを理解しています。 「教師教育教師教育大学は空ではない」という画面では、「Univerは空ではありません」という画面で表示できますか? – ExiRe

+1

Ah ... これで、ユーザーのエラーを尋ねています。あなたは@teacher_educationに直接質問しなければならないでしょう – bodacious

関連する問題