gpt4 book ai didi

javascript - rails 5 : Select2 Gem not working

转载 作者:行者123 更新时间:2023-12-01 05:17:28 25 4
gpt4 key购买 nike

我是 Rails 初学者,尝试在 Rails 应用程序中使用 Select2 gem,但我发现它没有按预期工作。仅在我期望其中包含搜索选项的情况下,它才显示为正常下拉列表。有人可以帮帮我吗?

<小时/>

HTML 代码

<tbody id="template">
<tr>
<td>
<%=
select_tag "order[order_placed][][itemname]",
options_from_collection_for_select(Stock.all, "item_name", "item_name"), {:class => 'form-control', :id => 'select_two', :style => "width: 180px"}
%>


</td>
<td><input name="order[order_placed][][quantity]" type="text" size='10' class="form-control" /></td>
<td><input name="order[order_placed][][unitprice]" type="text" size='10' class="form-control" /></td>
<td><input name="order[order_placed][][tax]" type="text" size='10' class="form-control"/></td>
<td><input name="order[order_placed][][discount]" type="text" size='10' class="form-control"/></td>
<td><input name="order[order_placed][][itemtotalprice]" type="text" size='10' class="form-control" /></td>
<td>
<button type="button" class="btn btn-default btn-sm sub" onClick="$(this).closest('tr').remove();">
<span class="glyphicon glyphicon-minus"></span>
</button>
</td>
</tr>
</tbody>
<小时/>

这是我的 application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require jquery
//= require toastr
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .

$(document).ready(function)(){
$('#select_two').select2();
}
<小时/>

Gem 文件具有相关的 gem

gem 'select2-rails'

最佳答案

我不知道这是什么解决方案,但在按照这些步骤操作后,我已在我的应用程序上手动进行了此操作

Gemfile

gem "select2-rails"
# bundle install

application.js

//= require select2

就在这个 //= require_tree . 行上方

application.js

(function($){
"use strict";
$(document).on('ready', function(){
$("#order_place_id").select2({
allowClear: true,
theme: "bootstrap"
});
});
}(jQuery));

应用程序.css

*= require select2
*= require select2-bootstrap

HTML 代码

<div class="form-group">
<label>Place Order</label>
<select name="order[order_placed][][itemname]" id="order_place_id" class="form-control">
<% Stock.all.each do |stock| %>
<option value="<%= stock.item_name %>">
<%= stock.item_name %>
</option>
<% end %>
</select>
</div>

最后,请参阅我实现的下图

enter image description here

如果您尝试此解决方案,请确保在完成步骤后重新启动服务器。

希望有帮助

关于javascript - rails 5 : Select2 Gem not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48243374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com