“建议” Rails 应用程序中文本框的功能

发布于 2024-07-06 08:55:09 字数 112 浏览 8 评论 0原文

我正在寻找一种最简单的方法来实现 Rails 应用程序中文本输入字段的“建议”功能。 这个想法是完成存储在数据库列中的名称,在用户键入时为用户提供可能匹配的下拉菜单。

感谢您的任何建议!

I'm looking for an easiest way how to implement the "suggest" feature for a text entry field in a Rails application. The idea is to complete names stored in a database column, giving the user a drop-down menu of possible matches as he types.

Thanks for any suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

国粹 2024-07-13 08:55:09

Rails 使用 text_field_with_auto_complete 方法使文本字段的“建议”式自动完成变得非常简单。

在 Rails 1.x 中,此方法内置于 ActionView::Helpers:: JavaScriptMacrosHelper,但对于 Rails 2.x,它已移至一个单独的插件< /a>.

假设您有一个名为 Post 的模型,其中有一个名为 title 的文本字段。 在您看来,通常使用 text_field_tag (或 f.text_field)的地方,只需使用 text_field_with_auto_complete 代替:

<%= text_field_with_auto_complete :post, :title %>

此外,在 PostsController 中,你必须做出相应的声明:

class PostsController < ApplicationController
  auto_complete_for :post, :title
end

这在幕后所做的就是向控制器动态添加一个名为 auto_complete_for_[object]_[method] 的操作。 在上面的示例中,此操作将被称为 auto_complete_for_post_title

值得指出的是,此自动生成操作使用的 find 调用将作用于所有模型对象,例如 Post.find(:all, ... )。 如果这不是您想要的行为(例如,如果想要根据登录用户将搜索限制为 Post 的特定子集),那么您必须定义自己的 控制器中的 auto_complete_for_[object]_[method] 操作。

Rails makes 'suggest'-style auto completion on text fields really easy using the text_field_with_auto_complete method.

In Rails 1.x this method was built into ActionView::Helpers::JavaScriptMacrosHelper, but for Rails 2.x it was moved to a separate plugin.

Let's say you have a model called Post which has a text field called title. In your view, where you would normally use text_field_tag (or f.text_field), just use text_field_with_auto_complete instead:

<%= text_field_with_auto_complete :post, :title %>

Additionally, in PostsController, you have to make a corresponding declaration:

class PostsController < ApplicationController
  auto_complete_for :post, :title
end

What this does behind the scenes is dynamically add an action called auto_complete_for_[object]_[method] to the controller. In the above example this action will be called auto_complete_for_post_title.

It's worth pointing out that the find call used by this auto-generated action will act across all model objects, e.g. Post.find(:all, ...). If this isn't the behaviour you want (for example, if want to restrict the search to a specific subset of Posts based on the logged in user) then you have to define your own auto_complete_for_[object]_[method] action in your controller.

み青杉依旧 2024-07-13 08:55:09

Ruby on Rails 具有可用的原型框架。 该框架由 Script.aculo.us 使用,它提供了一个 自动完成器控件 可能会提供您正在寻找的功能。

Ruby on Rails has the Prototype framework available. This framework is used by Script.aculo.us and it provides an autocompleter control that may provide the functionality that you're seeking.

×眷恋的温暖 2024-07-13 08:55:09

编辑:我将把这个答案留在这里作为一种理论参考点,但听起来像 自动完成器答案可能对您更有用:)

免责声明:虽然我在 Google 工作(它在各种 UI 中显然有“建议”元素),但我还没有看过任何围绕该区域的代码,甚至没有与任何人谈论客户端方面。

服务器端语言可能与这里无关。 重要的是客户端需要 AJAX。

我建议您有一个大约 1 秒的计时器(尝试找到最佳点),每次用户在文本框中输入击键时都会重置该计时器,如果用户离开文本框则该计时器会取消。 如果计时器触发,则让它触发 AJAX 请求。 AJAX 请求将包含用户目前输入的内容。 AJAX 响应应该是建议列表和原始请求文本。

如果当 AJAX 响应返回时,文本框中的文本仍然与响应中的字段相同(即用户此后没有键入过内容)并且文本框仍然具有焦点,则提供一个下拉列表。 (必须有数百个关于 HTML 组合框的示例页面才能完成这方面的工作。)

服务器所需要做的就是通过执行搜索并适当格式化响应来响应 AJAX 请求 - 这比客户端容易得多!

希望有所帮助 - 抱歉没有任何示例代码,但我怀疑它涉及很大,而且我并不是真正的 JavaScript 开发人员。

EDIT: I'll leave this answer here as a sort of theoretical reference point, but it sounds like the autocompleter answer is likely to be more useful to you :)

Disclaimer: Although I work for Google (which clearly has "Suggest" elements in various UIs) I haven't looked at any of the code around this area, nor even spoken to anyone about the client-side aspect.

The server-side language is probably irrelevant here. The important bit is the AJAX required on the client side.

I suggest you have a timer of about 1 second (experiment to find a sweet spot) which is reset each time the user enters a keystroke into the textbox and cancelled if the user navigates away from the textbox. If the timer fires, make it fire an AJAX request. The AJAX request would contain what the user has typed so far. The AJAX response should be the list of suggestions and the original request text.

If, when the AJAX response returns, the text in the textbox is still the same as the field in the response (i.e. the user hasn't typed since) and if the textbox still has focus, then offer a dropdown. (There must be hundreds of example pages about HTML comboboxes to do this side of things.)

All the server needs to do is respond to the AJAX request by performing the search and formatting the response appropriately - that's a lot easier than the client side!

Hope that helps - sorry to not have any sample code, but I suspect it's quite involved and I'm not really a JavaScript developer.

半﹌身腐败 2024-07-13 08:55:09

与 Rails 中的大多数事情一样,有多种方法可以做到这一点。 Rails 曾经包含一组内置的自动完成帮助程序,但该功能现在已被删除到 auto_complete 插件中。 这可能是您实现所需内容的最简单方法 - 控制器中的一个简单命令,您视图中的一些简单内容 - ta-dah。 要使用此解决方案,您只需将该插件安装到您的应用程序中 - 请参阅此页面,其中介绍了您需要了解安装并开始使用的所有信息。

正如另一个答案中所述,走这条路将利用与 Rails 捆绑在一起的原型和脚本化 AJAX 框架。 还有其他 AJAX 框架,特别是 JQuery,也可以帮助您完成此功能,但您将需要更多的学习来使用它们,而不仅仅是使用插件。

As with most things in Rails there are several ways to do this. Rails used to include a set of auto-completion helpers built-in, but that functionality has now been removed to the auto_complete plugin. This is likely the easiest way for you to get what you want implemented - a simple command in the controller, some simple stuff in your view - ta-dah. To use this solution you'll just need to install the plugin into your app - see this page which tells you all you need to know to get it installed and to get started.

As noted in another answer, going this route will make use of the prototype and scriptaculous AJAX frameworks that come bundled with Rails. There are other AJAX frameworks, notably JQuery, that can also help you accomplish this functionality, but you'll have more ramp-up learning to use them than just going with the plugin.

心碎无痕… 2024-07-13 08:55:09

您也可以尝试这种稍微手动的方法

# your_view.rhtml

<%= text_field 'contact', 'name', :id => 'suggest' %>
<div id='dropdown' style='display:none; z-index: 100; background: #FFFFFF'></div>

<script>
  new Ajax.Autocompleter('suggest', 'dropdown', "<%= url_for :controller => 'contacts', :action => 'suggest_name' %>") 
</script>

# contacts_controller.rb

def suggest_name
  query_string = params[:contact][:name]
  @contacts = Contact.find.all :conditions => ['name ilike ?', "%#{query_string}%"]
  render :partial => 'name_suggestions'
end

# contacts/_name_suggestions.rhtml

<ul>
<% for contact in @contacts %>
  <li><%= contact.name %></li>
<% end %>
</ul>

You can also try this slightly more manual approach

# your_view.rhtml

<%= text_field 'contact', 'name', :id => 'suggest' %>
<div id='dropdown' style='display:none; z-index: 100; background: #FFFFFF'></div>

<script>
  new Ajax.Autocompleter('suggest', 'dropdown', "<%= url_for :controller => 'contacts', :action => 'suggest_name' %>") 
</script>

# contacts_controller.rb

def suggest_name
  query_string = params[:contact][:name]
  @contacts = Contact.find.all :conditions => ['name ilike ?', "%#{query_string}%"]
  render :partial => 'name_suggestions'
end

# contacts/_name_suggestions.rhtml

<ul>
<% for contact in @contacts %>
  <li><%= contact.name %></li>
<% end %>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文