I am wondering how I should go about retrieving information from a mysql database associated with my ruby on rails app. When a user selects a product from the select box, I want to use the value of the selected option to retrieve the product's description and price from the database and then display it in a div. Using the following code:
<select>
<option value='1'>milk</option>
<option value='2'>eggs</option>
<option value='3'>butter</option>
</select>
<div id='product_description'></div>
<div id='product_price'></div>
When a user selects eggs for example, the first div would read: 'chicken babies' and the second div would read: '$2.00'.
My select tag is also populated from the database, if that is important to know.
The code to populate the select:
<select>
<option>Select Add-on</option>
<% for product in @products %>
<option value="<%= product.id %>"><%= product.item %></option>
<% end %>
</select>
I am also using jquery.
I guess I was basically wondering if I should just use an ajax call or if there was some magic rails way to accomplish the task. If I need to use ajax, can I just do it all from the js.coffee file? I can't figure out how to get the selected options value passed on to another file to select the correct row from the products table.
binding theChangeevent on yourselect, contacting the serveur for the selectedproduct_idand responds with apartial(or JSON, which is faster but needs to be handle on the client side to "parse" the response and update the view).options_for_selectto generate your options (apidock.com/rails/ActionView/Helpers/FormOptionsHelper/…) combined with aselect_tag(api.rubyonrails.org/classes/ActionView/Helpers/…) to generate the select ;) To get the selected product's id, you can do:$(this).val()(jQuery)params[:id](doraiser paramsto see what it contains)