Alrick Deillon
Alrick Deillon

Reputation: 119

Rails 3 : Check if record correctly updated when remote

I'm using javascript(coffeescript) as format for the response to an update action. I can't figure out how to check if the record has been successfully updated in my js.coffee response.

For create I use .new_record?, for destroy we have .destroyed? to check that the record has been created/destroyed correctly, what about update?

Upvotes: 0

Views: 1480

Answers (2)

Alrick Deillon
Alrick Deillon

Reputation: 119

If think the solution was too simple for me. I can simply use .errors.empty?..

Upvotes: 2

osahyoun
osahyoun

Reputation: 5231

What about returning different JSON objects, depending on the outcome of updating your object:

def update
  @foo = Foo.find(params[:id])
  respond_to do |format|
    if @foo.update_attributes(params[:foo])
      format.json { head :ok }
    else
      format.json { render json: @foo.errors, status: :unprocessable_entity }
    end
  end
end

Client side, you'd inspect the status property on the returned object.

Upvotes: 3

Related Questions