Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

95
Vistas
Get the id of a created object in rails using jquery

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

looks like you want json, not a template, instead of respond_with, why not try:

def createuserlink
  @new_element = Link.new(element_params)
  @new_element.save
  render json: @new_element.to_json
end
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda