I want to get the id of an element that I have created using a query-AJAX-POST-Request, but none of the approaches that I have found so far, had worked.
Currently I am stuck here
Controller:
def createuserlink
@new_element = Link.new(element_params)
@new_element.save
respond_with @new_element
end
Jquery:
$.ajax({
url: "links/createuserlink",
type: "POST",
data: my_data
}).done(function( data, textStatus, request ) {
alert(data);
});
The Request works and the new_element gets created, but the alert-message shows me the HTML-code of the new_element/show page. I can see the id of the returned page in the browser dev-tools (like foo.com/element/42
), but I don't know how to access it.
I tried to use getResponseHeader
as suggested here, but it only works for parameters (like foo.com?element=42
).
I also tried respond_with @new_element.id
, but he didn't liked the syntax.
What is the easiest and recommended way to get back the id?