rails的tags包:act-as-taggable-on
tags标签在内容管理系统里面是非常常见,也是很重要。你需要通过标签来给内容分类。
act-as-taggable-on非常的强大,github上4.8k的stars了.
下面简单介绍下
一个人会有多种类型的标签,比如:技能、兴趣、运动等,标签特别灵活,无法预先定义字段来存储,可能又加一个地域标签、或者方言标签等。
act-as-taggable-on 允许你详细指定一个随意数量的标签种类,
install and init
act-as-taggable-on非常的强大,github上4.8k的stars了.
下面简单介绍下
一个人会有多种类型的标签,比如:技能、兴趣、运动等,标签特别灵活,无法预先定义字段来存储,可能又加一个地域标签、或者方言标签等。
act-as-taggable-on 允许你详细指定一个随意数量的标签种类,
install and init
gem 'acts-as-taggable-on', '~>7.0'
bundle
rake acts_as_taggable_on_engine:intall:migrations
使用
class User < ActiveRecord::Base acts_as_taggable_on :tags acts_as_taggable_on :skills, :interests end class UsersController < ApplicationController def user_params params.require(:user).permit(:name, :tag_list) end end @user = User.new(:name => "Bobby")
Add and remove a single tag
@user.tag_list.add("awesome") # add a single tag. alias for << @user.tag_list.remove("awesome") # remove a single tag @user.save@user.tag_list.add("awesome", "slick") @user.tag_list.remove("awesome", "slick") @user.save
# add parse: true ,自动解析@user.tag_list.add("awesome, slick", parse: true) @user.tag_list.remove("awesome, slick", parse: true)#还有种写法@user.tag_list = "awesome, slick, hefty" @user.save @user.reload
With the defined context in model, you have multiple new methods at disposal to manage and view the tags in the context. For example, with :skill context these methods are added to the model: skill_list(and skill_list.add, skill_list.remove skill_list=), skills(plural), skill_counts.
保留tags的顺序的话,用的是
acts_as_ordered_taggable_on :tags
Finding most or least used tags
You can find the most or least used tags by using:
ActsAsTaggableOn::Tag.most_used ActsAsTaggableOn::Tag.least_used
You can also filter the results by passing the method a limit, however the default limit is 20.
ActsAsTaggableOn::Tag.most_used(10) ActsAsTaggableOn::Tag.least_used(10)
常用的也就这些,还有些设置tag的owner的见:https://github.com/mbleigh/acts-as-taggable-on
阅读量: 710
发布于:
修改于:
发布于:
修改于: