0

所以我正在尝试使用这个 gem和 simple_form 实现多个自动完成,但我得到了一个错误。

我试过这个:

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

"search"=>{"neighborhood_id"=>["Alley Park, Madison"],

所以它甚至没有使用这些值的 ID。

有没有人有任何想法?

编辑1:

针对@jvnill 的问题,我没有明确地params[:search]在控制器中做任何事情。搜索会创建一条新记录,并且正在搜索listings

在我的搜索控制器中,create操作,我只是这样做:

@search = Search.create!(params[:search])

然后我的search.rb(即搜索模型)有这个:

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
4

2 回答 2

1

首先,如果表单返回的是 id 而不是邻居的名称,这会更容易。我还没有使用过 gem,所以我不熟悉它是如何工作的。阅读自述文件说它会返回 ID,但我不知道为什么你只得到名字。我敢肯定,一旦您弄清楚如何返回 ID,您就可以更改下面的代码以适应它。

您需要在邻域和搜索之间创建一个连接表。我们称之为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

现在我们已经设置了关联,我们需要设置设置器和属性

# 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

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

# 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
于 2013-02-21T14:40:41.237 回答
0

您的问题是您如何从邻域模型中收集值

 Neighborhood.order(:name)

将返回一个名称数组,您还需要收集 id,但只显示名称使用 collect 并传递一个块,我相信这可能适合您

Neighborhood.collect {|n| [n.name, n.id]}

如果您想恢复剧院功能,请在 Neighborhood 类上声明一个范围以按名称对其进行排序,因为无论如何该行为也属于模型。

编辑>要向邻域模型添加范围/类方法,您通常会这样做

scope :desc, where("name DESC")

比你可以写这样的东西:

Neighborhood.desc.all

这将返回一个数组,从而允许 .collect 但还有其他方法可以获取 select 选项识别的那些 name 和 id 属性。

于 2013-02-14T13:14:15.343 回答