【问题标题】:ruby on rails ajax error handlingruby on rails ajax 错误处理
【发布时间】:2012-07-23 05:20:39
【问题描述】:

我正在使用 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

我该怎么做?

【问题讨论】:

    标签: jquery ruby-on-rails ajax error-handling


    【解决方案1】:

    试试这样的:

    @return = { :error => true, :response => @invitation.errors.full_messages.join(", ") }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2021-12-06
      • 2012-06-06
      • 1970-01-01
      • 2017-06-15
      相关资源
      最近更新 更多