I have a simple_form input field that is part of a form that I need to update asynchronously when the onchange event is triggered.
The field that I need to update is displayed as a select box since it is a collection. But the collection represents records of a nested set model. So when the user selects a particular value, I want to be able to update the same field with its children and give the user the option of picking any of the children as the value for the same field.
The problem is, how do I update just that field without touching the rest of the form.
Just to give some idea of my current setup: The form looks something like this:
<%= simple_form_for @product, html: {class: 'form-horizontal'} do |f| %>
<%= f.input :product_name %>
<%= f.association :location, collection: @locations, input_html: { data: {remote: :true, url: "/update_locations"} } %>
<%= f.submit 'Post', class: 'btn btn-default btn-lg' %>
<% end %>
@product is a new Product which belongs_to Location, @locations is a collection of Location each of which has_many Product hence the use of the simple_form association method Location also acts_as_nested_set
I have this in my routes.rb
get 'update_locations' => 'products#update_locations'
I need help in completing the controller action:
def update_locations
@product = Product.new
@location = Location.find_by_id(params[:product][:location_id])
@locations = @location.children
# TODO: render code that updates the simple_form field with
# the new @locations collection
end
I am not sure what code should go in the partial view that the controller above is supposed to render.