简体   繁体   English

如何在控制器导轨中显示collection_select中的选项

[英]How to display options in collection_select from controller rails

I have a scenario where upon selecting a designation , benefits will load in a drop down according to that designation . 我有一个方案,选择一个designation ,根据该designationbenefits于下拉菜单。 I have written an ajax call for this. 我为此写了一个ajax调用。

HTML: HTML:

<label>Designations</label>
<%= f.association :designation, input_html: { id: 'designation_select'},label_html: { class: 'form-control-plaintext' } %>

<label>Benefits</label>
<%= f.collection_select :benefit_ids, Benefit.all,:id, :name, {}, { id: 'benefit_select',multiple: 'multiple', :class => 'form-control multiselect-all',:include_blank => "Select Benefit" } %>

AJAX call: AJAX通话:

<script type="text/javascript">
  document.addEventListener("turbolinks:load", function() {

    $('#designation_select').change(function() {
      debugger
      id = $('#designation_select')[0].value;
      $.ajax({
          type: 'GET',
          dataType: 'html',
          data: {designation_id: id},
          url: '/benefits/benefit_by_designation',
          success: function(result) {
            debugger
            var el, length, options, parser, results, x;
            $('#benefit_select').empty();
            $('#benefit_select').prepend('<option value=\'\'> Select Benefit </option>');
            parser = new DOMParser;
            el = parser.parseFromString(result, 'text/html');
            options = el.getElementsByTagName('option');
            length = options.length;
            x = 0;
            results = [];
            while (x < length) {
              $('#designation_select')[0].appendChild(options[0]);
              results.push(x++);
            }
            return results;
          }
        });
    });
});
</script>

Method in Controller : 控制器中的方法:

  def benefit_by_designation
    benefits = Benefit.where('designation_id = ?', params[:designation_id])
    render html: view_context.options_from_collection_for_select(benefits, :id, :name, {include_blank: 'Select Benefit'})
  end

The code is running perfectly fine uptil the benefits = Benefit.where.. query in controller, but the render html: is not working as I wished it to. 该代码运行得非常好, benefits = Benefit.where..在控制器中运行了benefits = Benefit.where.. ,但是render html:无法正常工作。 How to display that benefits list in the dropdown? 如何在下拉菜单中显示该benefits列表?

EDIT: I had accidentaly set the datatype in ajax call as json but I have now set it to html . 编辑:我意外地将ajax调用中的datatype设置为json但现在将其设置为html Now, the success case of ajax call is being hit but still no results displayed. 现在,正在成功调用ajax的案例,但仍然没有结果显示。

try 尝试

def benefit_by_designation
 benefits = Benefit.where('designation_id = ?', params[:designation_id])
 render json: {:benefits => benefits }
end

change your ajax success as 将您的ajax成功更改为

success: function(result) {
         var options='';
          options += '<option value=\'\'> Select Benefit </option>';
          if(result.benefits)
            $.each(result.benefits,function(key, val){
              options += '<option value="'+val['id']+'">'+val['name']+'</option>';
            });
          var option_set = $('#benefit_select').html(options);
          option_set.trigger('change');
        }

hope this will help.. 希望这会有所帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM