问题描述
我的视图中有动态选择框
I have dynamic select boxes in my view
../ diys / _form.htmlerb
...
<%= 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), {}, { id: 'makes_select' } %><br>
<p>Select model</p>
<%= av.select :model, (render "make_models/make_model"), {}, { id: 'models_select' } %><br>
<% end %>
...
可在更改时启动咖啡脚本
which launches coffee script on change
../ assets / javascripts / diys.coffee
$ ->
$(document).on 'change', '#makes_select', (evt) ->
$.ajax 'update_make_models',
type: 'GET'
dataType: 'script'
data: {
make_id: $("#makes_select option:selected").val()
}
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
success: (data, textStatus, jqXHR) ->
console.log("Dynamic make select OK!")
启动
../ views / diys / update_make_models.coffee
$("#models_select").empty()
.append("<%= escape_javascript(render "make_models/make_model") %>")
可部分渲染
../ views / make_models / make_model.html .erb
<%= @models.collect do |models| %>
<option val="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>
一切都很好,除了鉴于我在option之后的select元素中得到了一组option close标签元素
。
and everything works great, except in view I get an array of option closing tags inside select element after option elementslike this.
这里还有我的部分
../ controllers / diys_controller.rb
...
def update_make_models
@models = MakeModel.where("make_id = ?", params[:make_id])
respond_to do |format|
format.js
end
end
def new
@diy = Diy.new
@step = @diy.steps.new
@diy.attached_vehicles.new
@step.add_images_to_steps.new
@makes = Make.all
@models = MakeModel.where("make_id = ?", Make.first.id)
end
...
推荐答案
<%= @models.collect do |models| %>
<option val="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>
应该是
<% @models.collect do |models| %>
<option val="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>
当使用<%=%>时,它将结果打印在模板文件中。确保将其排除在循环之外,否则您将获得这些工件。
When you use the <%= %> it prints the result in the template file. Make sure to leave it out of your loops or you will get these artifacts.
这里有一些。
这篇关于部分包含选项后获取选项关闭标签的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!