Getting array of option closing tags after partial containing options [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-12 03:55:20

问题


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!")

which launches

../views/diys/update_make_models.coffee

$("#models_select").empty()
   .append("<%= escape_javascript(render "make_models/make_model") %>")

which renders partial

../views/make_models/make_model.html.erb

<%= @models.collect do |models| %>
  <option val="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>

and everything works great, except in view I get an array of option closing tags inside select element after option elements like this.

Here's also some parts from my

../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
...

回答1:


<%= @models.collect do |models| %>
  <option val="<%= models.id %>"><%= models.make_model_name %></option>
<% end %>

Should be

<% @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.

Here is some great documentation on ERB tags.



来源:https://stackoverflow.com/questions/35397683/getting-array-of-option-closing-tags-after-partial-containing-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!