问题
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?
Gemfile
gem "ransack"
gem 'rails3-jquery-autocomplete'
gem 'nifty-generators'
gem 'jquery-rails', '~> 2.0'
in view:
<%= 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 %>
head of layout:
<%= 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' %>
controller
class ArticlesController < ApplicationController
autocomplete :article, :title
UPDATE 1 and routes.rb
get 'articles/autocomplete_article_title'
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.
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.
UPDATE 4 tried to use http://railscasts.com/episodes/102-auto-complete-association. but it's for older version of rails... and so on
UPDATE 5 changed in routes.rb
resources :articles do
get 'autocomplete_article_title'
end
No luck yet.
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?
UPDATE 7 so in view
f.text_field :title_start,
"data-autocomplete" => articles_autocomplete_article_title_path,
:size => 15
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
回答1:
Instead of
f.autocomplete_field :title_cont,
articles_autocomplete_article_title_path, :size => 15
Try
f.text_field :title_cont, "data-autocomplete" => articles_autocomplete_article_title_path
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
回答2:
Did 2 things: reroute routes (first in, fist out )
get 'articles/autocomplete_article_title'
get "news/index" , as:'news'
resources :users
resources :news
resources :articles
and 2) reroute permissions in controller
skip_before_filter :authorize, except: [:edit, :update, :destroy]
来源:https://stackoverflow.com/questions/12108197/using-autocomplete-in-app-with-ransack