【问题标题】:Rails 3 - My message instead of strange validation message of nested_attributes modelRails 3 - 我的消息而不是nested_attributes 模型的奇怪验证消息
【发布时间】:2012-03-22 15:10:21
【问题描述】:

我有模型 UserTeacherTeacherEducationTeacherEducation 属于 TeacherTeacher 属于 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"

而且我在 UserTeacher 模型中没有得到这么奇怪的名字。那么,我怎样才能显示正确的消息?

我的模型:

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


    【解决方案1】:

    错误消息告诉您教师的 TeacherEducation#teacher_education_university 属性为空。

    当您尝试保存用户时,它会返回@user.teacher.teacher_education 的teacher_education_university 属性的错误消息。

    因为您使用的是accepts_nested_attributes_for,所以如果@user 记录的教师无效,您将无法保存它。如果 TeacherEducation 无效,则无法保存 Teacher。

    【讨论】:

    • 我明白这一点。我可以在屏幕上显示Univer can't be empty. 而不是Teacher teacher education teacher education university can't be empty. 吗?
    • 啊...好吧,您是在询问用户的错误。您必须直接向@teacher_education 询问我认为的错误消息
    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2012-12-13
    • 2011-07-20
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多