【发布时间】:2012-01-25 15:53:23
【问题描述】:
我是 Rails 新手。我正在尝试通过自动完成来实现作为可标记的行为。我面临的问题是,如果我有一个用户的多个标签,自动完成会建议整个 tag_list 而不是单个标签。
这是我之前遇到的ActiveRecord::Relation issue 的后续问题,并在Taryn East 的帮助下解决了。
我希望自动建议仅向我推荐灰色和绿色标签,而不是灰色、白色。我认为它建议我使用灰色,白色,因为我之前为其他东西创建了这些标签,所以它采用整个数组而不是数组中的单个记录。
我怎样才能克服这个问题并要求它只建议我一个标签而不是多个标签?
这就是我得到的。
_form.html.erb
<%= form_for(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :tags %>
<%= f.autocomplete_field :tag_list, autocomplete_tag_name_users_path, :"data-delimiter" => ', ' %>
<% end %>
index.html.erb
<% @users.each do |user| %>
<h3><%= user.name %></h3>
<h4><i><%= user.tag_list %></i> | <%= time_ago_in_words(user.created_at) %> ago</h4>
<h4><%= link_to 'Show', user %> | <%= link_to 'Edit', edit_user_path(user) %> | <%= link_to 'Delete', user, confirm: 'Are you sure?', method: :delete %></h4>
<% end %>
<h1>Listing All Tags [<%= @smart.map(&:name).length %>]</h1>
<ul>
<% @smart.each do |tag| %>
<li> <%= tag.name %></li>
<% end %>
new.html.erb 是
<h1>New user</h1>
<%= render 'form' %>
<%= link_to 'Back', users_path %>
用户.rb
class User < ActiveRecord::Base
serialize :tags
acts_as_taggable_on :tags
scope :by_join_date, order("created_at DESC")
end
users_controller.rb
class UsersController < ApplicationController
autocomplete :tag, :name, :class_name => 'ActsAsTaggableOn::Tag'
def index
@users = User.all
@smart = User.tag_counts_on(:tags)
end
def new
@user = User.new
end
end
我怎样才能克服这个问题并要求它只建议我一个标签而不是多个标签?提前非常感谢。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 autocomplete rubygems acts-as-taggable-on