Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda