I want to know how to do the following with Ruby/Rails as an exercise.
index.html.erb
<div id="selectionsPane">
<label for="bootChooserControl">Boot style:</label>
<select id="bootChooserControl" name="bootStyle"></select>
</div>
<div id="productDetailPane">
</div>
</div>
<div>
<label>Item name:</label> <%= @boot.name %>
</div>
...
boots.js
$(function() {
$('#bootChooserControl').load('/fetch_boot_style_options');
$('#bootChooserControl').change(function(event){
$.get(
'/fetch_product_details',
{style: $(event.target).val()},
function(response) {
$('#productDetailPane').html(response);
$('[value=""]',event.target).remove();
}
);
});
});
boots_controller.rb
def fetch_product_details
@boot = Boot.where(sku: params[:style])
render layout: false
end
fetch_boot_style_options
<option value="">— choose a style —</option>
<% @boots.each do |boot| %>
<option value="<%= boot.sku %>"><%= boot.name %></option>
<% end %>
So I want Item Name populated with the correct boot name from my database. First, how do I use the select method to build my select button the Rails way? Second, when the user selects a different item I want the productDetailsPane to update. I don't know what I need to add to make this code Rails/AJAX ready.