[英]Rails Ajax: Search still refreshing the page after click
I am working on the search app using jquery ui rails gem.我正在使用 jquery ui rails gem 开发搜索应用程序。
Here's my set up.这是我的设置。
HTML: HTML:
<%= form_tag(contacts_path, class: "form-inline", role: "search", method: :get, remote: true) do %>
<%= text_field_tag :term, params[:term], class: "form-control search-input", id: "term", placeholder: "Search..", autocomplete: "off" %>
<%= hidden_field_tag :category_id, params[:category_id] %>
<button class="btn btn-primary btn-lg main-btn search-btn" type="submit">Search</button>
<% end %>
Notice that I place remote:true
in the form.请注意,我在表单中放置了
remote:true
。 And then on my contacts controller:然后在我的联系人控制器上:
def autocomplete
if params[:category_id] && !params[:category_id].empty?
category_find = Category.find(params[:category_id])
@contacts = category_find.contacts.search(params[:term]).order(created_at: :desc).page params[:page]
render json: @contacts.map { |contact| { id: contact.id, value: contact.name }}
else
@contacts = Contact.search(params[:term]).order(created_at: :desc).page params[:page]
render json: @contacts.map { |contact| { id: contact.id, value: contact.name }}
end
end
Then on my routes:然后在我的路线上:
scope '/dashboard' do
resources :contacts do
collection do
get 'autocomplete'
end
end
end
I've added an index.js.erb file to re-render my contacts list via my kaminari pagination gem:我添加了一个 index.js.erb 文件以通过我的 kaminari 分页 gem 重新呈现我的联系人列表:
$('#contacts').html('<%= j(render @contact) %>');
$('#paginator').html('<%= j(paginate(@contacts, remote: true)) %>');
What I am expecting to happen here is that since I place a remote:true
on my search bar I expect when I click in an autocomplete item it will not refresh the page but right now it is still refreshing the page:我期望在这里发生的是,因为我在搜索栏上放置了一个
remote:true
我希望当我单击自动完成项时它不会刷新页面但现在它仍在刷新页面:
Any idea why is this so?知道为什么会这样吗?
您的render
方法将刷新您的页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.