我有一个带有多对多关联的帖子和标记模型:
class Post < ActiveRecord::Base
attr_accessible :title, :content
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
end
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :posts, :through => :taggings
has_many :subscriptions
has_many :subscribed_users, :source => :user, :through => :subscriptions
end
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
我正在使用rails3-jquery-autocomplete:
posts_controller.rb:
autocomplete:tag,:name
routes.rb中:
resources :posts do
get :autocomplete_tag_name, :on => :collection
end
_form.html.erb:
<div class="field">
<%= f.label :tag_names %><br />
<%= f.autocomplete_field :tag_names, autocomplete_tag_name_posts_path, :"data-delimiter" => ' ' %>
</div>
我工作正常,但我希望自动完成下拉列表显示带有特定标记的帖子数量。像这样:
---------------------
| dr |
---------------------
|drinks (23) |
|drugs (4) |
| |
---------------------
(非常像StackOverflow)
我很感激任何见解或示例代码。