自定义 ActiveModel full_messages
Customise ActiveModel full_messages
我想从我的自定义验证消息中删除该属性并只显示该消息,而不是
School Please Provide Your School Name
我要return
Please Provide Your School Name
在我这里的模型中设置
validates :school, presence: { message: 'Please Provide Your School Name' }
消息被 return 编辑为 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_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
full_message
需要两个参数 attribute
和 message
.
更新:避免猴子修补并编辑您的语言环境文件以使其具有类似内容:
en:
errors:
format: "%{message}"
我想从我的自定义验证消息中删除该属性并只显示该消息,而不是
School Please Provide Your School Name
我要return
Please Provide Your School Name
在我这里的模型中设置
validates :school, presence: { message: 'Please Provide Your School Name' }
消息被 return 编辑为 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_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
full_message
需要两个参数 attribute
和 message
.
更新:避免猴子修补并编辑您的语言环境文件以使其具有类似内容:
en:
errors:
format: "%{message}"