【发布时间】:2016-10-18 04:31:46
【问题描述】:
问题
请阅读编辑,它更干净一些。
select_tag 使用 Select2 不获取/发送 ajax 请求。
我正在尝试使用 ajax 为我的 select_tag 获取数据,因为表中有很多行。
<h3> Ajax send requests but I have Client.all </h3>
<%= invoice_form.collection_select :client_id, Client.all, :id, :name %>
<h3> Ajax not working </h3>
<input id="invoice_seller_id" />
这些标签与Select2 库一起使用来替换选择框。
从<%= invoice_form.collection_select :client_id, Client.all, :id, :name %> 生成的第一个input 发送正确的json 请求,甚至得到正确的响应。
但是有信息找不到结果。
在第二种方法中,浏览器甚至不发送 ajax 请求。
代码
发票/_form.html.erb<h3> Ajax send requests but I have Client.all </h3>
<%= invoice_form.collection_select :client_id, Client.all, :id, :name %>
<h3> Ajax not working </h3>
<input id="invoice_seller_id" />
invoice.js.coffee
jQuery ->
$('#invoice_client_id').select2
theme: 'bootstrap'
ajax:
url: '/clients.json'
dataType: 'json'
results: (data, page) ->
{ results: $.map(data, (client, i) ->
{
id: client.id
text: client.name
}
) }
$('#invoice_seller_id').select2
theme: 'bootstrap'
ajax:
url: '/clients.json'
dataType: 'json'
results: (data, page) ->
{ results: $.map(data, (client, i) ->
{
id: client.id
text: client.name
}
) }
问题
为什么在第一个 input 结果没有插入到 select_tag。为什么第二个输入不发送任何请求?
我正在尝试重新创建这个:Using Select2 with Ruby on Rails
编辑 1
我只是将这两个输入字段更改为一个。
<h3> Ajax not working </h3>
<%= invoice_form.collection_select :seller_id, [], :id, :name %>
它现在正在发送 ajax 请求并获得正确的响应,但不插入数据以选择选项并仍然显示 未找到结果
【问题讨论】:
标签: jquery ruby-on-rails ajax ruby-on-rails-4 coffeescript