我正在关注关于 jquery tokeninput 的 railscasts 第 258 集。
不知道为什么用户的输入没有达到用户查询的输入参数?
我的数据库是postgres。
药房管制员
def index
@pharmacies = Pharmacy.order(:name)
respond_to do |format|
format.html
format.json { render json: @pharmacies.tokens(params[:q]) }
end
end
用户模型
def pharmacy_tokens=(tokens)
self.pharmacy_ids = Pharmacy.ids_from_tokens(tokens)
end
药房模型
def self.tokens(query)
pharmacies = where('lower(name) LIKE ? or name LIKE ?', "%#{query}%", "%#{query}%")
if pharmacies.empty?
[{id: "<<<#{query}>>>", name: "New: \"#{query}\""}]
else
pharmacies
end
end
用户视图
<div class="field">
<%= f.label :pharmacy_tokens, "Pharmacies" %><br />
<%= f.text_field :pharmacy_tokens, data: {load: @user.pharmacies} %>
</div>
用户的.js.coffee
jQuery ->
$('#user_pharmacy_tokens').tokenInput '/pharmacies.json'
theme: 'facebook'
prePopulate: $('#user_pharmacy_tokens').data('load')
preventDuplicates: true
queryParam: "q"
结果
Processing by PharmaciesController#index as JSON
Parameters: {"locale"=>"en"}
(0.6ms) SELECT COUNT(*) FROM "pharmacies" WHERE (lower(name) LIKE '%%' or name LIKE '%%')
Pharmacy Load (0.8ms) SELECT "pharmacies".* FROM "pharmacies" WHERE (lower(name) LIKE '%%' or name LIKE '%%') ORDER BY pharmacies.name ASC, name
Completed 200 OK in 30ms (Views: 27.0ms | ActiveRecord: 1.4ms)
当在 text_field 中输入 javascript 工作时,只是文本不再出现在查询中......只是我空的“喜欢”百分号......“%%”。我在这里的输入发生了什么?
先感谢您。