I am new to rails and I have a form below where I need to update a text field(car price) using the option selected in the select box (adjust price). drop down needs to have multiple percentage option like 100% , 50% and 25%. when one percentage is selected then it should update the car price in the field with new calculated adjusted price.
<% car = Car.find(params[:id]) %>
<table width="100%">
<tr style="vertical-align: top;">
<td><b>Car Model</b></td>
<td><b>Price</b></td>
<td><b>Adjusted price</b></td>
</tr>
<tr style="vertical-align: top;">
<td><%= text_field(‘Car’, ‘car_name’, size: 10, value: check_for_car_name(car.name)) %></td>
<td><%= text_field(‘Car’, ‘price’, size: 10, value: check_for_car_price(car.price)) %></td>
<td><%= select(adjusted price(not sure of this part)) %></td>
</tr>
</table>
I am not sure about the select box that how I can update the car price on the fly ? Thanks in advance.
CodePudding user response:
Have a look at rails guides, you can use options_for_select
combined with select_tag
<%= select_tag(:city_id, options_for_select([["25%", 1], ["50%", 2]])) %>
this will output
<option value="1">25%</option>
<option value="2">50%</option>
Now, to update the price dynamically you'll probably want to use javascript in your front-end for example by adding an event listener to your select. Then, when selected option is for example 50% you can do calculations directly on the price field by changing it's value.
document.getElementById('price').value = calculated price
or as the question was marked for jQuery you can use .val()
$("#price").val() = calculated price