How to pass params[:search] with autocomplete_field_tag?

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I am using the Crowdint rails3 jquery autocomplete and having trouble with my search form.

This is how my search form looks without 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%>

Now when I change around the form for autocomplete and search:

<%= 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%>

This will not work if I have params[:search] inside of my autocomplete_field_tag:

ActionView::Template::Error (wrong number of arguments (5 for 4))

How do I set the search parameter so I can actually search with autocomplete?


More info:

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]

回答1:

(Disclaimer: I've no experience with this particular gem, the following answer is based on a brief look through it's source only.)

The autocomplete_field_tag method takes five parameters according to the source, the last one being an options hash:

autocomplete_field_tag(name, value, source, options = {})

So, given that your controller method e.g. needs a parameter called product_name your method call should probably read:

autocomplete_field_tag('product_name', '', search_autocomplete_product_name_path, :placeholder => "Search for a Product.....", :id => "main-search-field")


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!