【发布时间】:2018-10-31 04:06:12
【问题描述】:
我最近升级到 Rails 5.2 和 ruby 2.5.1,我的选择框停止工作。
我必须更改我的 admin.js 中的要求文件并添加完整版本才能查看选择框
//= require select2-full
但是现在获取数据的 ajax 查询甚至初始选择都没有显示任何结果。我正在使用Gon 将数据传递给我的 js,并且我检查了 gon 是否具有正确的值。我的js.coffee如下:
clientFormatResult = (client) ->
client.email
clientFormatSelection = (client) ->
client.email
$ ->
$select_input = $('#publish_to')
$select_input.val(' ')
$select_input.select2({
width: '420px'
height: 'auto'
placeholder: "Search"
minimumInputLength: 2
multiple:true
closeOnSelect: true
tokenSeparators:[',',' ']
ajax: {
url: $select_input.data('search-endpoint')
data: (term, page) ->
{
q: term # search term
page_limit: 10
}
results: (data, page) -> # parse the results into the format expected by Select2.
console.log("result")
# since we are using custom formatting functions we do not need to alter remote JSON data
results: data.manage_clients
}
initSelection: (element, callback) ->
callback(gon.published_to)
formatResult: clientFormatResult
formatSelection: clientFormatSelection
})
我的控制器搜索端点:
def search
clients = Client.where("email like '%#{params[:q]}%'").map { |c| { :id => c.id, :email => c.email } }
render :json => clients
end
private
def client_params
params.require(:client).permit(:email, :password, :password_confirmation, :first_name, :last_name, :organisation, :assigned_team)
end
搜索端点结果:
{
"manage_clients": [
{
"id": 46,
"email": "aaaa@example.net"
},
{
"id": 365,
"email": "bbbb@example.org"
}
]
}
选择输入所在的视图源代码摘录:
<div class="input string required publish_to">
<label class="string required control-label" for="report_clients">
Publish to
</label>
<input data-search-endpoint="/my-endpoint-search" id="publish_to" name="publish_to" type="hidden" value="" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true"><span class="select2 select2-container select2-container--default" dir="ltr" style="width: 420px;"><span class="selection"><span class="select2-selection select2-selection--multiple" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1"><ul class="select2-selection__rendered"><li class="select2-search select2-search--inline"><input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Search for a client" style="width: 418px;"></li></ul></span></span><span class="dropdown-wrapper" aria-hidden="true"></span></span>
<div>
<a class="add_button button" href="/some_url">Action</a>
</div>
</div>
我已经尝试了将控制器响应的解决方案suggested here
但没有变化。
我还尝试将dataType: 'json' 作为suggested here 添加到ajax 调用中
在第一个 select2 的末尾添加.select2('val',[])
我尝试手动将{ id: '1', text:'example' } 添加到initSelection
我在控制台中没有收到任何错误
但是 ajax 调用似乎不起作用。在控制台中没有对搜索端点的调用。
【问题讨论】:
-
@debugger 我在另一个操作中定义了 client_params,这会影响搜索操作吗?
-
@dbugger 我已经添加了代码
-
@dbugger 如果我手动调用搜索操作,无论有无参数,它都可以正常工作
-
@dbugger 来自日志,select2 没有运行 ajax 调用,也没有运行 initSelection
-
@marimaf 您能否分享您的 select2 所在的生成页面的完整代码?
标签: ruby-on-rails ruby-on-rails-5 jquery-select2