使rails3-autocomplete-jquery gem与带有多个输入的Simple_Form很好地配合 [英] Getting rails3-autocomplete-jquery gem to work nicely with Simple_Form with multiple inputs
问题描述
因此,我尝试使用此gem 和simple_form和am实现多个自动完成功能出现错误.
So I am trying to implement multiple autocomplete using this gem and simple_form and am getting an error.
我尝试过:
<%= f.input_field :neighborhood_id, collection: Neighborhood.order(:name), :url => autocomplete_neighborhood_name_searches_path, :as => :autocomplete, 'data-delimiter' => ',', :multiple => true, :class => "span8" %>
这是我得到的错误:
undefined method `to_i' for ["Alley Park, Madison"]:Array
在我的参数中,它是在neighborhood_id
中发送的:
In my params, it is sending this in neighborhood_id
:
"search"=>{"neighborhood_id"=>["Alley Park, Madison"],
因此,这些值甚至都没有使用ID.
So it isn't even using the IDs for those values.
有人有什么想法吗?
对于@jvnill的问题,我没有明确地对控制器中的params[:search]
做任何事情.搜索将创建一条新记录,并且正在搜索listings
.
In response to @jvnill's question, I am not explicitly doing anything with params[:search]
in the controller. A search creates a new record, and is searching listings
.
在我的Searches Controller中,create
操作中,我只是在这样做:
In my Searches Controller, create
action, I am simply doing this:
@search = Search.create!(params[:search])
然后我的search.rb
(即搜索模型)具有以下内容:
Then my search.rb
(i.e. search model) has this:
def listings
@listings ||= find_listings
end
private
def find_listings
key = "%#{keywords}%"
listings = Listing.order(:headline)
listings = listings.includes(:neighborhood).where("listings.headline like ? or neighborhoods.name like ?", key, key) if keywords.present?
listings = listings.where(neighborhood_id: neighborhood_id) if neighborhood_id.present?
#truncated for brevity
listings
end
推荐答案
首先,如果表单返回ID而不是邻居的名称,这会更容易.我还没有使用过宝石,所以我不知道它是如何工作的.阅读自述文件说它将返回ID,但我不知道为什么您只得到名字.我敢肯定,一旦您弄清楚如何返回ID,就可以更改下面的代码以适应这种情况.
First of all, this would be easier if the form is returning the ids instead of the name of the neighborhood. I haven't used the gem yet so I'm not familiar how it works. Reading on the readme says that it will return ids but i don't know why you're only getting names. I'm sure once you figure out how to return the ids, you'll be able to change the code below to suit that.
您需要在邻域和搜索之间创建联接表.我们称其为search_neighborhoods.
You need to create a join table between a neighborhood and a search. Let's call that search_neighborhoods.
rails g model search_neighborhood neighborhood_id:integer search_id:integer
# dont forget to add indexes in the migration
之后,您要设置模型.
# search.rb
has_many :search_neighborhoods
has_many :neighborhoods, through: :search_neighborhoods
# search_neighborhood.rb
belongs_to :search
belongs_to :neighborhood
# neighborhood.rb
has_many :search_neighborhoods
has_many :searches, through: :search_neighborhoods
现在我们已经设置了关联,我们需要设置设置器和属性
Now that we've setup the associations, we need to setup the setters and the attributes
# search.rb
attr_accessible :neighborhood_names
# this will return a list of neighborhood names which is usefull with prepopulating
def neighborhood_names
neighborhoods.map(&:name).join(',')
end
# we will use this to find the ids of the neighborhoods given their names
# this will be called when you call create!
def neighborhood_names=(names)
names.split(',').each do |name|
next if name.blank?
if neighborhood = Neighborhood.find_by_name(name)
search_neighborhoods.build neighborhood_id: neighborhood.id
end
end
end
# view
# you need to change your autocomplete to use the getter method
<%= f.input :neighborhood_names, url: autocomplete_neighborhood_name_searches_path, as: :autocomplete, input_html: { data: { delimiter: ',', multiple: true, class: "span8" } %>
最后但并非最不重要的是更新find_listings
last but not the least is to update find_listings
def find_listings
key = "%#{keywords}%"
listings = Listing.order(:headline).includes(:neighborhood)
if keywords.present?
listings = listings.where("listings.headline LIKE :key OR neighborhoods.name LIKE :key", { key: "#{keywords}")
end
if neighborhoods.exists?
listings = listings.where(neighborhood_id: neighborhood_ids)
end
listings
end
就这样:)
更新:使用f.input_field
UPDATE: using f.input_field
# view
<%= f.input_field :neighborhood_names, url: autocomplete_neighborhood_name_searches_path, as: :autocomplete, data: { delimiter: ',' }, multiple: true, class: "span8" %>
# model
# we need to put [0] because it returns an array with a single element containing
# the string of comma separated neighborhoods
def neighborhood_names=(names)
names[0].split(',').each do |name|
next if name.blank?
if neighborhood = Neighborhood.find_by_name(name)
search_neighborhoods.build neighborhood_id: neighborhood.id
end
end
end
这篇关于使rails3-autocomplete-jquery gem与带有多个输入的Simple_Form很好地配合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!