我有这行代码
render json: { message: t(:incorrect_password) }, status: :unauthorized
导致此回复......
{
"message": "Please re-enter your password. The password entered is incorrect."
}
但我希望响应看起来像这样......
{
"message": [
"Please re-enter your password. The password entered is incorrect."
]
}
实现此目的的正确方法是什么,将结果作为数组的元素而不是单个实体返回?
答案 0 :(得分:2)
正如上面的评论所暗示的那样,你可以简单地在数据文本周围包装一个数组文字,就像这样......
render json: { message: [t(:incorrect_password)] }, status: :unauthorized
或者,您可以通过调用Array
来调用它,就像这样......
render json: { message: Array(t(:incorrect_password)) }, status: :unauthorized