我使用的是Hyperstack.org框架,所以使用Opal编译的Ruby代码。Hyperstack与Rails的集成在客户端创建了一些模型的表示,我有一个关于响应中的错误验证的问题。
当使用验证器保存模型时,当其中一个验证器被触发时,我无法在promise响应中获得完整的错误消息。
在此代码中:
@User.save(validate: true).then do |result|
if result[:success]
puts 'successs'
mutate @open = false
else
result[:models].each do |response_model|
puts "response_model.errors.class = #{response_model.errors.class}" #ActiveModel::Errors
puts "response_model.errors.full_messages = #{response_model.errors.full_messages}" #nothing puts
end
end第一个but返回#ActiveModel::Errors,但我似乎无法使用该模型的方法。
我可以看到这个的测试:https://github.com/hyperstack-org/hyperstack/blob/a09bc601b3ad289c9f75156416ed39dff88059c9/ruby/hyper-model/spec/batch1/misc/errors,所以我希望它能工作,那一定是我!
此外,我还注意到对promise的JSON响应实际上包含错误消息:
{
"success" => false, "saved_models" => [
[227154, "User", {
"id" => 48,
"first_name" => "ds",
"last_name" => nil,
"email" => nil,
"image_src" => nil,
"date_of_birth" => nil,
"is_admin" => false,
"is_female" => false,
"is_enabled" => true,
"created_at" => "2019-03-23T12:29:05.728Z",
"updated_at" => "2019-03-23T12:29:05.728Z"
}, {
"last_name" => ["can't be blank"]
}]
], "message" => "HyperModel saving records failed!", "models" => [ < User: 0x37752(0x37750)[errors {
"last_name" => ["can't be blank"]
}] > ]
}感谢任何帮助!
发布于 2019-03-24 06:18:41
看起来您在使用full_messages方法时遇到了HyperModel中的错误。AFAIK所有其他方法在您的示例中都可以很好地工作。
如果您确实需要full_messages,请查看https://github.com/hyperstack-org/hyperstack/issues/143,有一个替代补丁可供应用
https://stackoverflow.com/questions/55316897
复制相似问题