在带有ransack的应用程序中使用自动完成功能 [英] using autocomplete in app with ransack
问题描述
美好的一天. 我遇到了一个控制器,该控制器使用ransack按标题查找文章. 我需要用于查找信息的字段以使用自动完成功能. 目前它可以,但是没有自动完成功能,您能帮我解决这个问题吗 和gem https://github.com/crowdint/rails3-jquery-autocomplete-app?
Good day. I hame a controller that uses ransack to find articles by their title. I need field that's used for finding info to use autocomplete. Currently it finds alright, but no autocomplete, could you assist me in that problem and gem https://github.com/crowdint/rails3-jquery-autocomplete-app?
宝石文件
gem "ransack"
gem 'rails3-jquery-autocomplete'
gem 'nifty-generators'
gem 'jquery-rails', '~> 2.0'
视图中:
<%= search_form_for @q do |f| %>
<%= f.label t('.find') %>
<%= f.autocomplete_field :title_cont,
articles_autocomplete_article_title_path, :size => 15 %><br />
<%= f.submit :value=>'Search', :class=>'fl_right' %>
<% end %>
布局负责人:
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag '/javascripts/jquery-ui-1.8.23.custom.min.js',
'/javascripts/autocomplete-rails.js', '/javascripts/rails.js' %>
<%= stylesheet_link_tag '/stylesheets/ui-lightness/jquery-ui-1.8.23.custom.css' %>
控制器
class ArticlesController < ApplicationController
autocomplete :article, :title
更新1 和routes.rb
UPDATE 1 and routes.rb
get 'articles/autocomplete_article_title'
更新2
在视图中,代替<%= f.autocomplete_field :title_cont,
用过的
1):title_start没有变化
2):articles_article_title_start等导致错误.
UPDATE 2
in view , in place of <%= f.autocomplete_field :title_cont,
used
1 ) :title_start no changes
2) :articles_article_title_start and so on lead to errors.
更新3 我已经编辑了routes.rb.将"articles/autocomplete_article_title"移到了区域设置变量的范围内,现在可以正常工作了. ..出现了autocomplete下拉列表...但为空:(必须立即修复该问题.
UPDATE 3 i've edited routes.rb. moved get 'articles/autocomplete_article_title' into scope of locale variable, now it kind of works... autocomplete drop down list appears... but empty :( have to fix that one now.
更新4 尝试使用 http://railscasts.com/episodes/102-auto-complete-association .但这是用于较旧版本的rails ...等等
UPDATE 4 tried to use http://railscasts.com/episodes/102-auto-complete-association. but it's for older version of rails... and so on
更新5 改变了routes.rb
UPDATE 5 changed in routes.rb
resources :articles do
get 'autocomplete_article_title'
end
还没有运气.
更新6 据我了解,当前的问题是:洗劫&自动完成功能使用一个字段. 适用于搜查autocomplete_field类型的字段.但是我不知道如何通过自动完成的ransack工作使字段名称可接受.你能帮忙吗?
UPDATE 6 As far as i understand current problem is: ransack & autocomplete use one field. It's suitable for ransack that field of autocomplete_field type. but i don't know how to make field name acceptable by ransack work with autocomplete. Could you help?
更新7 因此,鉴于
f.text_field :title_start,
"data-autocomplete" => articles_autocomplete_article_title_path,
:size => 15
在routes.rb中
in routes.rb
scope '(:locale)' do
resources :users
resources :news
resources :articles do
end
get 'articles/autocomplete_article_title'
get "news/index" , as: 'news'
# match "/news*tail" => 'news#index'
root :to => 'News#index', as: 'news'
end
推荐答案
代替
f.autocomplete_field :title_cont,
articles_autocomplete_article_title_path, :size => 15
尝试
f.text_field :title_cont, "data-autocomplete" => articles_autocomplete_article_title_path
在我的项目中,我使用了meta_search和simple_form,就像
In my project i used meta_search and simple_form and it was like
f.input :country_name_equals, url: autocomplete_country_name_path, :as => :autocomplete
这篇关于在带有ransack的应用程序中使用自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!