Ruby on rails 自定义ActiveModel完整消息

Ruby on rails 自定义ActiveModel完整消息,ruby-on-rails,ruby,rspec,activemodel,Ruby On Rails,Ruby,Rspec,Activemodel,我想从自定义验证消息中删除该属性,只显示该消息,而不是 School Please Provide Your School Name 我想回去 Please Provide Your School Name 正如我在这里的模型所设定的 validates :school, presence: { message: 'Please Provide Your School Name' } 该消息作为JSON响应返回 查看full_messages方法 # File activemodel/li

我想从自定义验证消息中删除该属性,只显示该消息,而不是

School Please Provide Your School Name
我想回去

Please Provide Your School Name
正如我在这里的模型所设定的

validates :school, presence: { message: 'Please Provide Your School Name' }
该消息作为JSON响应返回

查看full_messages方法

# File activemodel/lib/active_model/errors.rb, line 348
def full_messages
  map { |attribute, message| full_message(attribute, message) }
end
我可以用这个覆盖吗

# File activemodel/lib/active_model/errors.rb, line 348
def full_messages
  map { |attribute, message| full_message(message) }
end
我试过这个

module ActiveModel
  class Errors
    def full_messages
      map { |attribute, message| full_message(message) }
    end
  end
end
位于
/lib/active\u model/errors.rb

但是当我尝试运行我的测试(rspec)时,我得到了错误

/home/richardlewis/.rvm/gems/ruby-2.2.0@lnf_api/gems/activemodel-4.2.0/lib/active_model/validations.rb:297:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
我将文件加载到application.rb中

config.autoload_paths += %W(#{config.root}/lib)
我怎样才能解决这个问题

谢谢

编辑

控制器

class RegistrationsController < Devise::RegistrationsController
skip_before_action :verify_authenticity_token
respond_to :json

def create
 @user = User.new(registration_params)
   if @user.valid?
     @user.save
     render json: { message: I18n.t("devise.registrations.signed_up_but_unconfirmed") }, status: 201
   else
     render json: { message: @user.errors.full_messages }, status: :unprocessable_entity
   end
end

protected
def registration_params
  json_params = ActionController::Parameters.new(JSON.parse(request.body.read))
  json_params.require(:user).permit(:username, :school, :email, :password, :password_confirmation)
 end
end
类注册控制器
完整消息
需要两个参数
属性
消息

更新:避免猴子补丁,并编辑您的区域设置文件,使其具有以下内容:

en:
  errors:
    format: "%{message}"

您在测试中提到了问题-您能提供导致错误的代码吗?抱歉,我的措辞不正确..当我运行rspec时,不会因为该错误而运行任何测试确定,现在我知道问题来自何处了。您是否可以添加控制器的代码,其中会为您生成意外消息?我是否需要在控制器中要求/加载/包含模块?我正在获取
load_missing_constant':无法自动加载常量ActiveModel::Errors,需要/home/richardlewis/Rails/lnf_api/lib/active_model/Errors.rb来定义它(LoadError)
是的,与覆盖现有行为相比,这将是一个更好的方法。我每秒钟都会收到语法错误,但每行有两个空格,不是吗?