, , simple_form.
AJAX , , HTML JS , HTML, JavaScript.
jQuery replaceWith .
, , .
JavaScript, AJAX. .html.erb, simple_form . JS jQuery replaceWith() , .
JS partial (location.js.erb):
var updatedLocations = $('#product_location_id', $('<%= j render "updateLocations" %>'))
$('#product_location_id').replaceWith(updatedLocations)
HTML erb partial (updateLocations.html.erb) :
<%= simple_form_for @product, html: {class: 'form-horizontal'} do |f| %>
<%= f.association :location, collection: @locations, input_html: { data: {update_locations: "true", remote: :true, url: "/update_locations"} } %>
<% end %>
:
def update_locations
@product = Product.new
@location = Location.find_by_id(params[:product][:location_id])
@locations = @location.children
if @locations.empty?
render nothing: true
else
render partial: 'locations'
end
end