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

  • Home
  • SEARCH
  • 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 7085181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:23:35+00:00 2026-05-28T07:23:35+00:00

Say that a Rails app has a Car and an Order model. The Car

  • 0

Say that a Rails app has a Car and an Order model. The Car model contains name and color fields. The Order model contains a car_id field, and a color field so that the user can specify an alternative color if required.

When a user creates a new Order, they can select the Car name from a select field. How do I dynamically update a text field with the color on this event.

The Order form contains the fields

<select id="order_car_id></select>
<input id="order_color"></input>

The Car show view responds to json

def show
    respond_to do |format|
      format.json { render :json => @car.to_json, :status => 200 }
    end
end 

and this function responds to the event and grabs the json array.

$(function (){
    $('#order_car_id').live("change", function(e) {
    e.preventDefault();
      $.ajax({
        url: "/cars/" + $(this).val(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            $('#order_color').val(car[0].color);
        },
        error: function(xhr,exception,status) {
          //error functions...
        }
      });
    });
});

but I’m getting

Uncaught ReferenceError: car is not defined

What is the correct syntax for passing a value from the json array into the #order_color field? I’ve tried various permutations of $('#order_color').val(car[0].color); but can’t seem to figure it out.

Grateful if anyone can point me in the right direction and learn how this works.

Thank you!

  • 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-28T07:23:36+00:00Added an answer on May 28, 2026 at 7:23 am

    Looks like your success function, you don’t define is what car is.

    Your function is passing in ‘data’, but ‘car’ is not defined anywhere in that function.

    You ned to extract car from the data object.

    First, add console.log(data) to your success function so you can see the structure of the data response.

    function(data) {
      console.log(data);
      //$('#order_color').val(car[0].color);
    }
    

    You’ll probably get back something like:

    data:{
      car:{
        color:"blue"
        size:"large"
      }
    }
    

    Once you know the format of your data object, you can access by:

    $('#order_color').val(data.car.color);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When building a rails app that allows a User to login and create data,
I've created an app that has several models (say A, B) that are polymorphically
Lets say have a main app, that also has a blog, I like to
As my Rails app matures, it's becoming increasingly apparent that it has a strong
To make a long explanation short, suffice it to say that my Rails app
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
Lets say I have a Rails app that is modelled as follows: There is
In a Ruby on Rails app, say I have an Item and an Order
I heard Ryan Bates say that the Rails JavaScript helper methods prevent unobtrusive JavaScript.
I am having a problem with structuring rails application. Let's say that I have

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.