0

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.

Community
  • 1
  • 1
DaveInFL
  • 162
  • 2
  • 9
  • as far as i understand, you want to update a text-field for the price to have the value of the selected plan-price? – phoet May 07 '14 at 01:28
  • That is correct, yes. – DaveInFL May 07 '14 at 01:49
  • i'd recommend storing all the price objects as json on a html node and change the value of the input field accordingly. the proposed solution using ajax is way to complex for this simple task – phoet May 07 '14 at 02:09
  • phoet, I am still new to rails, can you provide perhaps an example? I'd have no idea where to even start on this. – DaveInFL May 07 '14 at 13:28

1 Answers1

0

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.

Oscar Valdez Esquea
  • 890
  • 1
  • 11
  • 26
  • I could not get this to work for me Oscar. I have tried to enter the details of what I have done but the comments box is not big enough :( Anyway, followed what you had but it didn't work. The price did not populate in the field once I had changed the price list. – DaveInFL May 15 '14 at 18:38
  • Have you checked if there's any JS error? Also, im sure this is not the most practical way of doing this, but its the only one i can think of. – Oscar Valdez Esquea May 15 '14 at 22:52
  • I didn't see any errors, but I am still quite new to rails and perhaps I was looking in the wrong place. I basically redesigned the form now to handle this, not the most elegant way but the best way I could come up with. I was hoping phoet would elaborate on his answer. – DaveInFL May 16 '14 at 15:05
  • Thanks. Not using JS, just getting the user to save information first on the first few fields, and then it throws them into edit mode and that is when I populate the price. Like I said, not the most elegant but works :) – DaveInFL May 18 '14 at 17:02