基於維迪亞的答案,發現與其他問題的結果,這是我做的:
在Foo的控制器
補充說:
def checkFooExists
@foo = Foo.find_by_foo_id params[:foo_id]
if [email protected]?
flash[:notice] = 'You entered an existing Foo ID, so here is the edit page'
flash.keep(:notice)
render :js => "window.location = '" + edit_foo_path(@foo) + "'"
else
render :nothing => true, :status => 409
end
end
在路線改變:
#resources :foos
resources :foos do
collection do
get 'checkFooExists'
end
end
中的應用.js,在自動完成事件處理程序中爲文本框選擇添加一條jQuery獲取行:
select: function(event, ui) {
$.get("/foos/checkFooExists", { foo_id: ui.item.foo_id });
$('#bar').val(ui.item.bar);
return false;
}
謝謝,Vidya。我意識到我應該澄清foo_id不是記錄的主鍵。 ID是。有沒有辦法按照你的建議發送foo_id作爲參數? – user1279437
這不會改變任何東西。我編輯了我的答案,希望更清楚。 – Vidya