Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
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?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

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
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!