Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:23:43+00:00 2026-05-22T14:23:43+00:00

This might seem like a beginner question, but it has me stumped. I have

  • 0

This might seem like a beginner question, but it has me stumped. I have a User model and a Location model. A location belongs to a user and a user has many locations. I have locations stored as a nested resource in my routes file.

Here’s the create function in my LocationsController file:

  def create
    @user = User.find(params[:user_id])
    @location = @user.locations.build(params[:location])

    respond_to do |format|
      if @location.save
        format.html { redirect_to @user }
        format.js { 
          @locations = @user.locations
          render 'create' 
        }
      else
        format.html { render @user }
        format.js
      end
    end
  end

create.js.erb:

$('table#locations').html("<%= escape_javascript(render(@locations)) %>");

_location.html.erb:

<tr>
  <td><%= location.user.name %></td>
  <td><%= location.created_at %></td>
  <td><%= location.longitude %></td>
  <td><%= location.latitude %></td>
</tr>

Saving to the database using AJAX works without a problem. However, when the record is retrieved, the last row of the returned table only includes the user name. The created_at, longitude and latitude are not displayed. If I refresh the page then they are displayed. Does anybody have any ideas why this might be happening?

Thanks.

Update

I reversed the order of the models and it’s still the bottom entry that isn’t printing. My guess is it’s something to do with the views and not the models.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:23:44+00:00Added an answer on May 22, 2026 at 2:23 pm

    I think the problem may be arising when you initialize your @locations variable in the controller for the AJAX response. You save the new location, but perhaps the @user object doesn’t notice. Instead of

    @locations = @user.locations
    

    try

    @locations = Location.where(:user_id => @user)
    

    or something like that, which will guarantee that you’re pulling the locations from the database.

    It’s a guess!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: This might seem like a Super User question at first, but please read
This might seem like a simple question. But I have been looking for an
This might seem like a stupid question I admit. But I'm in a small
This might seem like a weird question, but bear with me. I'd like to
Ok sorry this might seem like a dumb question but I cannot figure this
This might seem like a silly question but valgrind doesn't by default give you
This might seem like a really dumb question, but I am writing an application
This might seem like a stupid question but it's been a long day. I'm
This might seem a bit like a do-my-homework-for-me question (and it is), but I
This might seem like a silly question, but after asking some questions on stackoverflow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.