1
我正在關注有關jquery tokeninput的第57集的第52集。jquery tokeninput輸入參數丟失
我不知道爲什麼用戶的輸入沒有達到用戶查詢的輸入參數?
我的數據庫是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>
users's.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的作品,它只是文字不再查詢......只是我的空「樣'百分號..'%%'。我的輸入發生了什麼事?
謝謝你提前。
都不工作,當刪除雙引號的where子句正則表達式不再有效。我嘗試了單引號,雖然它是一個有效的正則表達式,字符串NOT查詢參數被傳遞到查詢中..'SELECT COUNT(*)FROM「pharmacies」WHERE(lower(name)LIKE'%#{query} %'或名稱LIKE'%#{query}%')' –
是導致此問題的區域設置參數嗎? '處理PharmaciesController#index爲JSON 參數:{「locale」=>「en」} [1m [36mPharmacy Load(1.0ms)[0m [1mSELECT「pharmacies」。* FROM「pharmacies」WHERE(name ILIKE' %')ORDER BY pharmacies.name ASC,name [0m 在30ms內完成200 OK(查看:28.0ms | ActiveRecord:1.0ms)' –
您是否在控制器中獲得了params [:q]? – Kashyap