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 7184751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:15:56+00:00 2026-05-28T18:15:56+00:00

In a Ruby on Rails app, say I have an Item and an Order

  • 0

In a Ruby on Rails app, say I have an Item and an Order model as follows:

class Item
  :name
  :default_price
end 

class Order
  :customer
  :item_id
  :order_price
end

On an Order form, I can automatically populate the order_price field with the default_price using javascript and a json call as follows:

$(function (){  
    $('#order_item_id').live("change", function(e) {
    e.preventDefault();
      $.ajax({
        url: "/items/" + $(this).val(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(data) {   
        $('#order_price').val(data.item.default_price);
        },
        error: function(xhr,exception,status) {
          //errors....
        }
      });
    });
});

So far, so good. But now suppose I want to be able to list multiple items under a single order. In that case I could use a nested form and the following tables:

class Item
  :name
  :default_price
end 

class Order
  :customer
end

class OrderItem
   :order_id
   :item_id
   :order_price
end

The problem is that each nested OrderItem record has the same select ID, and a random order_price ID, eg:

<div class="fields">
  <ol>
    <li>  //first nested record
      <select id="order_item_id"></select>
      <input id="order_orderitems_attributes_0_orderprice"></input>
    </li>
    <li>  //second nested record
      <select id="order_item_id"></select>
      <input id="order_orderitems_attributes_###randomnumber###_orderprice"></input>
    </li>
  </ol>
</div>

Clearly this causes problems for the javascript function.

How can I get my nested forms to play nicely with my javascript, so that selecting an item populates the corresponding price field?

I will need to pass an identifier for the nested record into the function so that the function runs when the select field is changed. I will also need to somehow identify the correct order-price field.

Anyone have any ideas?

Thanks!!!

EDIT

Actually, I can get this working by using the .next() selector to find the next DOM element. i.e., in the js function

$('#order_item_id').live("change", function(e) {
    var current_price = $(this).next('.order-price')[0];
    .....
    success: function(data) {   
            $('current_price').val(data.item.default_price);
            },
    .....

but I’m feeling uncomfortable that each select field has the same ID. Is this something I should be concerned about? Are there any issues I should be thinking about?

EDIT 2

order form

<%= nested_form_for @order do |f| %>
    .....order fields
    <ol>
        <%= f.fields_for :orderitems %>         
    </ol>
    <p><%= f.link_to_add new_ico, :orderitems %></p>
    <p><%= f.submit %></p>
<% end %>

orderitems partial

<li>
    <%= select(:orderitem,:item_id,@items.collect{|s|[s.name, s.id]},:prompt=>"select") %>
    <%= f.text_field :order_price, :class => "order-price" %>
</li>
  • 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-28T18:15:57+00:00Added an answer on May 28, 2026 at 6:15 pm

    So you want changing the selected item for a select update the input right after it, right? If so, you can change your success handler to something like this:

    function(data) {   
      // this refers to the select that its value has changed
      // and next('input') finds the input right after it
      $(this).next('input').val(data.item.default_price);
    }
    

    In order to make it more explicit that the input contains a specific value such as the order price, you can assign an HTML 5 data- attribute to it like this:

    <%= f.input :order_price, :"data-field" => "order-price" %>
    

    This way the CSS selector in your JavaScript would become a little bit more intuitive:

    function(data) {   
      // this refers to the select that its value has changed
      // and next('input') finds the input right after it
      $(this).next('input[data-field=order-price]').val(data.item.default_price);
    }
    

    Worth mentioning that the data- attributes work even in HTML 4 if you are using a recent version of jQuery.

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

Sidebar

Related Questions

Background: So I have roughly (Ruby on Rails app) class A def calculate_input_datetimes #
In my Ruby on Rails app, I have a User table and a Foo
I have a ruby on rails app running a server and sometimes it needs
I have a Ruby on Rails app running on my server, and I can't
I have a Ruby on Rails app that I've recently deployed to a remote
I have a Rails 3.1 app, running on Ruby 1.9.2, Mongo 2.0.2, using Mongoid
I have Ruby on Rails app (3.1rc4) and I am constantly getting a couple
In my Ruby on Rails app, I've got: class AdminController < ApplicationController def create
I have a ruby on rails app that uses Heroku. I have the need
I have a Ruby/Rails app, which is standard Rails CRUD, with some jQuery and

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.