Q: Rails: Return value from another table after a select I had a question that was marked as a duplicate but it really didn't answer my question, but it did answer another question that I had so that was great :) I think perhaps I did not explain myself correctly so going to try again.  Please keep in mind that I am still relatively new to RoR.  My previous question can be found here: 
Rails: Select on 1 field returns value to another field
So, let me explain what I am trying to achieve again.  I have a table called plans and a table called plan_prices.  A plan has many plan prices.  I have another table called contracts.  A contract can have a plan and a plan price, so the contract table has plan_id and plan_price_id.  So here is what I am trying to achieve.  When I create a contract I have a select for the plan which returns the plan_id.  I also have a select on the plan_price that returns the plan_price_id.  This first select has some JS on it to determine when it changes so that the plan_price is filtered accordingly.  All this is working just fine.  The final piece that I want to do is when I select the plan_price there is a column on that table called price.  I want to take this price and default it into the contract.price field which can then be changed if the user deems necessary.  I am just having a hard time working out how I would default the plan_price.price value into the contract.price field once the plan_price has been selected.  The contract.price field is a simple free entry field and not a select field, since the user can change the price to something else.
Hope the above is clear and I hope that there is someone that can help me out on this.
TIA.

A: Send an Ajax request on the onchange state of the plan_price field:
:onchange=>"selectPrize(this)"

You will be passing the selected value as a param on your AJAX request
JS Function   
 function selectPrize(prizeID) {    
        var dataString ='prize='+prizeID.value;
        $.ajax({  
        type: "GET",  
        url: "/your_controller/your_controller_method",  
        data: dataString  
         });          
         return false;
    };

On your controller, in the selected method, look for the plan prize with the id you got through your ajax request.
@plan_prize = Planprize.where("id = #{params[:prize]}").first

Then create a js.erb file in your folder, depending on the method you've chosen in your controller and do some simple javascript to change the value of the contract_prize:
your_method.js.erb file
$(document.getElementById('contract_prize').value = '<%= @plan_prize.prize %>');

This should work.
