获取Rails3中,自动完成,jQuery的宝石多输入与Simple_Form很好地工作(Getti

2019-11-01 13:43发布

所以我想实现使用多个自动完成这种宝石和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

Answer 1:

首先,如果形式返回ID,而不是附近的名称,这将是更容易。 我没有使用过的宝石又是那么我不熟悉它是如何工作的。 阅读上的自述说,它会返回IDS,但我不知道为什么你只得到名称。 我敢肯定,一旦你弄清楚如何返回的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


Answer 2:

你的问题是,如何你从附近收集型值

 Neighborhood.order(:name)

将返回名称数组,你还需要收集ID,但只显示名称中使用收集和传递块,我beleive这可能OWRK你

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

声明对邻里类范围的名字,如果你想获得theat功能回来,因为这种行为也是在新型属于好歹来订购吧。

编辑>要在范围/类方法添加到邻域模型,你通常做soemthing这样

scope :desc, where("name DESC")

比你写的东西,如:

Neighborhood.desc.all

它会返回一个阵列,从而使.collect但也有其他的方式来获得由选择的选项承认的名称和id属性。



文章来源: Getting rails3-autocomplete-jquery gem to work nicely with Simple_Form with multiple inputs