【问题标题】:Rails autocomplete with acts-as-taggable-on issueRails 自动完成,带有作为可标记的问题
【发布时间】: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


    【解决方案1】:

    我建议你看看这个Railscasts。它几乎可以满足您的需求。

    【讨论】:

    • 感谢 Railscasts 视频提示!我看过视频,但 Ryan 的解释更像是使用保存的单个标签(作者),一次一个,但我的允许在保存信息时保存多个标签。我想显示单个标签稍后在查询中匹配的内容,而不是显示之前保存的整个数组!
    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2023-03-20
    • 2013-11-08
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多