我正在使用Crowdint rails3 jquery autocomplete我的搜索表单出现问题。
这是我的搜索
表单在没有自动完成的情况下的样子:
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search], :placeholder => "Search for a Product.....", :id => "main-search-field" %>
<%= submit_tag "Search", :name => nil, :id => "main-search-field-button" %>
<%end%>
现在,当我更改自动完成和搜索的表单时:
<%= form_tag search_path, :method => 'get' do %>
<%= autocomplete_field_tag 'name','', search_autocomplete_product_name_path, params[:search], :placeholder => "Search for a Product.....", :id => "main-search-field" %>
<%= submit_tag "Search", :name => nil, :id => "main-search-field-button" %>
<%end%>
如果我的 autocomplete_field_tag
中有 params[:search]
,这将不起作用:
ActionView::Template::Error (wrong number of arguments (5 for 4))
如何设置搜索参数,以便我可以实际使用自动完成功能进行搜索?
更多信息:
class SearchController < ApplicationController
autocomplete :product, :name, :full => true
# Sunspot search.
def index
@search = Product.search do
fulltext params[:search]
paginate(:per_page => 1, :page => params[:page])
end
@products = @search.results
end
end
# routes.rb
get 'search/autocomplete_product_name'
resources :search, :only => [:index]
最佳答案
(免责声明:我对这个特定的 gem 没有经验,以下答案仅基于对其源代码的简要浏览。)
autocomplete_field_tag
方法根据来源采用五个参数,最后一个是选项哈希:
autocomplete_field_tag(name, value, source, options = {})
因此,考虑到您的 Controller 方法,例如需要一个名为 product_name
的参数,您的方法调用可能应为:
autocomplete_field_tag('product_name', '', search_autocomplete_product_name_path, :placeholder => "Search for a Product.....", :id => "main-search-field")
关于ruby-on-rails - 如何传递参数[:search] with autocomplete_field_tag?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106902/