如何为habtm字段实现具有自动完成功能的文本字段?

时间:2022-10-05 14:31:52

I tried the example from Rails Cookbook and managed to get it to work. However the text_field_with_auto_complete works only for one value.

我尝试了Rails Cookbook中的示例并设法使其工作。但是text_field_with_auto_complete仅适用于一个值。

class Expense < ActiveRecord::Base
  has_and_belongs_to_many :categories
end

In the New Expense View rhtml

在新费用视图rhtml中

<%= text_field_with_auto_complete :category, :name %>

Auto complete works for the first category. How do I get it working for multiple categories? e.g. Category1, Category2
Intended behavior: like the * Tags textbox

自动完成适用于第一类。如何让它适用于多个类别?例如Category1,Category2预期行为:类似*标签文本框

Update:
With some help and some more tinkering, I got multiple comma-seperated autocomplete to show up (will post code-sample here).
However on selection, the last value replaces the content of the text_field_with_auto_complete. So instead of Category1, Category2.. the textbox shows Category2 when the second Category is selected via auto-complete. Any ideas how to rectify this?

更新:通过一些帮助和一些修补,我得到多个逗号分隔自动完成显示(将在此处发布代码示例)。但是在选择时,最后一个值将替换text_field_with_auto_complete的内容。因此,当通过自动完成选择第二个类别时,文本框显示Category2,而不是Category1,Category2 ..任何想法如何纠正这个?

4 个解决方案

#1


1  

I think this blog post covers what you're looking for: AJAX autocompletion with Rails.

我认为这篇博客文章涵盖了您正在寻找的内容:使用Rails进行AJAX自动完成。

#2


1  

If you are just trying to support multiple instances of autocomplete per field, you can pass a delimiter to the autocomplete options with the symbol :token. This provides a delimiter to allow multiple results. * would use :token => ' ' (there should be a space between the quotes, but the autoformat is removing it) to specify space at the delimiter between multiple takes although ',' is more commonly used.

如果您只是尝试为每个字段支持多个自动完成实例,则可以使用符号:token将分隔符传递给自动完成选项。这提供了一个允许多个结果的分隔符。 *将使用:token =>''(引号之间应该有一个空格,但autoformat正在删除它)以在多个引号之间的分隔符处指定空格,尽管','更常用。

#3


0  

This is not quite your question, but I wouldn't recommend using HABTM anymore. You should create a join model and use has_many :through. (In your case you'd create a new model called ExpenseCategoryAssignment, or something)

这不是你的问题,但我不建议再使用HABTM了。您应该创建一个连接模型并使用has_many:through。 (在你的情况下你会创建一个名为ExpenseCategoryAssignment的新模型,或者其他东西)

The problem is that HABTM creates ambiguities that rails doesn't like, and it tends to expose bugs you wouldn't see otherwise.

问题是HABTM会产生rails不喜欢的歧义,并且它往往会暴露出你不会看到的错误。

#4


0  

You need to use "data-delimiter" param like this
<%= f.autocomplete_field :brand_name, welcome_autocomplete_brand_name_path, "data-delimiter" => ', ' %>

您需要使用“data-delimiter”参数,例如<%= f.autocomplete_field:brand_name,welcome_autocomplete_brand_name_path,“data-delimiter”=>','%>

#1


1  

I think this blog post covers what you're looking for: AJAX autocompletion with Rails.

我认为这篇博客文章涵盖了您正在寻找的内容:使用Rails进行AJAX自动完成。

#2


1  

If you are just trying to support multiple instances of autocomplete per field, you can pass a delimiter to the autocomplete options with the symbol :token. This provides a delimiter to allow multiple results. * would use :token => ' ' (there should be a space between the quotes, but the autoformat is removing it) to specify space at the delimiter between multiple takes although ',' is more commonly used.

如果您只是尝试为每个字段支持多个自动完成实例,则可以使用符号:token将分隔符传递给自动完成选项。这提供了一个允许多个结果的分隔符。 *将使用:token =>''(引号之间应该有一个空格,但autoformat正在删除它)以在多个引号之间的分隔符处指定空格,尽管','更常用。

#3


0  

This is not quite your question, but I wouldn't recommend using HABTM anymore. You should create a join model and use has_many :through. (In your case you'd create a new model called ExpenseCategoryAssignment, or something)

这不是你的问题,但我不建议再使用HABTM了。您应该创建一个连接模型并使用has_many:through。 (在你的情况下你会创建一个名为ExpenseCategoryAssignment的新模型,或者其他东西)

The problem is that HABTM creates ambiguities that rails doesn't like, and it tends to expose bugs you wouldn't see otherwise.

问题是HABTM会产生rails不喜欢的歧义,并且它往往会暴露出你不会看到的错误。

#4


0  

You need to use "data-delimiter" param like this
<%= f.autocomplete_field :brand_name, welcome_autocomplete_brand_name_path, "data-delimiter" => ', ' %>

您需要使用“data-delimiter”参数,例如<%= f.autocomplete_field:brand_name,welcome_autocomplete_brand_name_path,“data-delimiter”=>','%>