0

* Still need help on this. The duplicate that people have suggested does not answer my question. It answers one of my questions but not this one. I have taken a look at the duplicate that people have recommended and that certainly helped me, in that now I have some jQuery code that detects when the value changes and I can see the actual value, but how do I then set the value of the other field in jQuery (the price) since I need to call some SQL to find the price based upon the item ID that was changed right? So, how I get the price once I have the item id? The price is not a drop down, which is what is being suggested, it is simply a data entry field that I want to default once the item is picked. *

I am trying to work out how I can have a select on one field in rails, which returns a certain value into this field but also returns another value to another field.

For example, I want to select a certain item so the select shows the item name but returns the id into the underlying field value. That is fine, no problems with that. But, I also have a price field that I'd like to populate with the item price once the item is selected in field one. I don't think this is possible using the select helper in rails correct?

Is there a good way of doing this? I am still quite new to rails so please be gentle :)

Here is what I have, but it isn't working :(

<%= f.select :id, Item.where('id = ?', ":item_id").order('price ASC').all.map {|ref| [ref.price, ref.id]}, { include_blank: false, required: true} %>

:item_id is in the previous field and is also chosen from a select.

DaveInFL
  • 162
  • 2
  • 9
  • 2
    care to show us some code? – Raj Apr 24 '14 at 14:25
  • You'll need to use javascript to detect a change in the first select and then load the new options into the second. – Mike Campbell Apr 24 '14 at 14:51
  • So, I have to use JS to do this? It cannot be done in native rails? – DaveInFL Apr 24 '14 at 15:30
  • there is no such thing as "native rails" it's just a bunch of conventions in a framework. javascript is one part of the framework in terms of asset-pipeline and coffeescript. – phoet Apr 25 '14 at 03:07
  • Ok. Let me rephrase the question then. Is JS the only way to do this then? If so could someone please provide a little more detail on how it could be accomplished? – DaveInFL Apr 25 '14 at 16:47

0 Answers0