如何使用 act_as_taggable_on rails 显示所有标签
How to display all of tags using act_as_taggable_on rails
我正在使用 Rails 5.1,
我使用 acts-as-taggable-on gem
https://rubygems.org/gems/acts-as-taggable-on 在我的 post 模型中。
I want to ask how to display all list of my tags inside my posts index
?
例如。我有名为鞋子、钱包、手表的标签。
我想在我的 post 索引中显示这些标签。
当我点击鞋子时,它会呈现所有与鞋子标签链接的 post。
我的架构:
create_table "taggings", id: :serial, force: :cascade do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.string "tagger_type"
t.integer "tagger_id"
t.string "context", limit: 128
t.datetime "created_at"
t.index ["context"], name: "index_taggings_on_context"
t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
end
我的post控制器
def index
@posts = Post.list_for(params[:page], params[:tag]).search(params[:search])
end
谢谢你帮助我。
检索所有标签:
ActsAsTaggableOn::Tag.all
查看此 link 了解更多信息:https://github.com/mbleigh/acts-as-taggable-on/wiki/Tutorial-for-Posts#controller
也可以通过上下文检索标签,检查这个:https://github.com/mbleigh/acts-as-taggable-on/wiki/Get-all-tags-by-context
注意!
ActsAsTaggableOn::Tag
是一个简单的ActiveRecord模型。所以你可以像这样调用查询:
existing_posts_tags = ActsAsTaggableOn::Tag.joins(:taggings).where(taggings: { teggable_type: 'Post' })
我正在使用 Rails 5.1, 我使用 acts-as-taggable-on gem https://rubygems.org/gems/acts-as-taggable-on 在我的 post 模型中。
I want to ask how to display all list of my tags inside my posts index ?
例如。我有名为鞋子、钱包、手表的标签。 我想在我的 post 索引中显示这些标签。 当我点击鞋子时,它会呈现所有与鞋子标签链接的 post。
我的架构:
create_table "taggings", id: :serial, force: :cascade do |t|
t.integer "tag_id"
t.string "taggable_type"
t.integer "taggable_id"
t.string "tagger_type"
t.integer "tagger_id"
t.string "context", limit: 128
t.datetime "created_at"
t.index ["context"], name: "index_taggings_on_context"
t.index ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
t.index ["tag_id"], name: "index_taggings_on_tag_id"
t.index ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
t.index ["taggable_id", "taggable_type", "tagger_id", "context"], name: "taggings_idy"
t.index ["taggable_id"], name: "index_taggings_on_taggable_id"
t.index ["taggable_type"], name: "index_taggings_on_taggable_type"
t.index ["tagger_id", "tagger_type"], name: "index_taggings_on_tagger_id_and_tagger_type"
t.index ["tagger_id"], name: "index_taggings_on_tagger_id"
end
我的post控制器
def index
@posts = Post.list_for(params[:page], params[:tag]).search(params[:search])
end
谢谢你帮助我。
检索所有标签:
ActsAsTaggableOn::Tag.all
查看此 link 了解更多信息:https://github.com/mbleigh/acts-as-taggable-on/wiki/Tutorial-for-Posts#controller
也可以通过上下文检索标签,检查这个:https://github.com/mbleigh/acts-as-taggable-on/wiki/Get-all-tags-by-context
注意!
ActsAsTaggableOn::Tag
是一个简单的ActiveRecord模型。所以你可以像这样调用查询:
existing_posts_tags = ActsAsTaggableOn::Tag.joins(:taggings).where(taggings: { teggable_type: 'Post' })