我有一些基本的模型验证,这些验证在 AJAX 提交表单时触发。如果验证失败,我想将验证错误传递回 View ,以便我可以告诉用户。
def save
logger.debug( params )
@video = Video.new( video_params )
if @video.save
render json: @video
else
render json: errors.messages
end
end
这会引发错误,因为 errors
未定义。我究竟做错了什么?我读了the docs on this它只显示 View 中使用的 errors.messages
。
最佳答案
errors 是来自 ActiveRecord
的实例方法。在你的情况下正确的使用方法是这样的:
@video.errors.messages
关于ruby-on-rails - rails 4 : How to check validation errors from inside a controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598674/