$.ajax
url: "/models"
type: "POST"
dataType: 'json'
data: {model: {name: "name", x: "x", y: "y"}}
Is there any way to check if my server eccepted this request and saved new element without making server request again?
Oh .. I figured it out.
Every time when ajax request is triggered it waits till server respond with some data. (in my example it is data in json format)
My ajax request in coffescript looks like this now:
$.ajax
url: "/models"
type: "POST"
dataType: 'json'
data: {model: {name: "name", x: "x", y: "y"}}
success: (data) ->
alert "Request was send to the server and server axcepted what I wanted to say … but I don't know if he do what I've told to him"
error: (data) ->
alert "The server says that he couldn't do what I've told him becouse of error number: #{JSON.stringify(data['status'])}"
And the most important in this case is to make server actually respond with some value of an expecting error. So … some changes needs to be done in Controller. I wanted to create some element so action "create" looks like this:
def create
@model = Model.new(model_params)
respond_to do |format|
if @model.save
format.html { render :nothing => true }
format.json { render :json => @model, :status => :ok}
else
format.html { ender :nothing => true }
format.json { render json: @model.errors, status: :unprocessable_entity }
end
end
end
:unprocessable_entity sends error #422