I have this coffeescript function for dynamic select boxes, and I need to pass "modelsSelect" content to this other script, but I just can't get it working.
diys.coffee
dynamicSelect = (makesSelect, modelsSelect) ->
$(document).on 'change', makesSelect, (evt) ->
$.ajax 'update_make_models',
type: 'GET'
dataType: 'script'
data: {
make_id: $("#{makesSelect} option:selected").val(),
'models': modelsSelect
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic make select OK!")
dynamicSelect '#diy_attached_vehicles_attributes_0_make', '#diy_attached_vehicles_attributes_0_model'
dynamicSelect '#diy_attached_vehicles_attributes_1_make', '#diy_attached_vehicles_attributes_1_model'
update_make_models.coffee
$(models).empty()
.append("<%= escape_javascript(render "make_models/make_model") %>")
make_models/_make_models.html.erb
<% @models.collect do |models| %>
<option value="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>
------------------------------------------------------------------------------------------------------------------------------------ Edit
Heres part of my form containig those select boxes
<div class="vehicle_field">
<%= f.fields_for :attached_vehicles do |av| %>
<p>Select make</p>
<%= av.select :make, options_for_select(@makes.collect { |make|[make.make_name, make.id] }, 0), {} %><br>
<p>Select model</p>
<%= av.select :model, (render "make_models/make_model"), {} %><br>
...
<% end %>
</div>
Figured it out!
diys.coffee
dynamicSelect = (makesSelect, modelsSelect) ->
$(document).on 'change', makesSelect, (evt) ->
$.ajax 'update_make_models',
type: 'GET'
dataType: 'script'
data: {
make_id: $("#{makesSelect} option:selected").val(),
model_div_id: modelsSelect
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic make select OK!")
dynamicSelect '#diy_attached_vehicles_attributes_0_make', '#diy_attached_vehicles_attributes_0_model'
dynamicSelect '#diy_attached_vehicles_attributes_1_make', '#diy_attached_vehicles_attributes_1_model'
diys_controller.rb
def update_make_models
@models = MakeModel.where("make_id = ?", params[:make_id])
@modelsid = params[:model_div_id]
respond_to do |format|
format.js
end
end
update_make_models.coffee
$("<%= @modelsid %>").empty()
.append("<%= escape_javascript(render "make_models/make_model") %>")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.