stergosz
stergosz

Reputation: 5860

ruby on rails ajax error handling

I am getting started into ajax with ruby and my problem is that i cant get the error messages i could get when i submit the form via post with refresh

controller

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

so now i have custom errors but i would like to replace "BAD" with the actual errors that the form returns...

model

class Invite < ActiveRecord::Base

    validates :email, :presence => true, :uniqueness => true

    validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

end

how could i do that?

Upvotes: 0

Views: 845

Answers (1)

MurifoX
MurifoX

Reputation: 15089

Try something like this:

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

Upvotes: 1

Related Questions