0

I have models User, Teacher, TeacherEducation. TeacherEducation belongs to Teacher, Teacher belongs to User. I use nested attributes to save everything via one line in my controller. But i get very strange names of fields for TeacherEducation when my app shows validation errors. For example:

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

I don't have Teacher teacher education teacher education university name, i set my own alias in locales/en.yml

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

And i don't get such strange names in User or Teacher models. So, how can i show right message?

My models:

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

My controller:

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

My hash:

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

1

The error message is telling you that the Teacher's TeacherEducation#teacher_education_university attribute is blank.

When you try to save the User, it's returning the error message for @user.teacher.teacher_education's teacher_education_university attribute.

Because you're using accepts_nested_attributes_for, you can't save the @user record if it's Teacher is not valid. You can't save the Teacher if it's TeacherEducation is not valid.

Sign up to request clarification or add additional context in comments.

2 Comments

I understand that. Can i show on the screen Univer can't be empty. instead of Teacher teacher education teacher education university can't be empty. ?
Ah... Well, you're asking for the errors on User. You'll have to ask the @teacher_education directly for it's error message I think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.