我正在使用 ruby 开始使用 ajax,我的问题是当我通过刷新提交表单时,我无法收到错误消息
控制器
class InvitesController < ApplicationController
def request_invite
render_404 unless request.xhr?
@invitation = Invite.new(params[:invite])
if @invitation.save
@return = { :error => false, :response => "OK" }
else
@return = { :error => true, :response => "BAD" }
end
render :json => ActiveSupport::JSON.encode( @return )
end
end
所以现在我有自定义错误,但我想用表单返回的实际错误替换“BAD”......
模型
class Invite < ActiveRecord::Base
validates :email, :presence => true, :uniqueness => true
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
end
我怎么能这样做?