在Ruby on Rails中为帖子添加标签

时间:2022-07-05 07:31:59

I am creating a blog in Rails using Scaffolding. I want to add a 'tags' field on each post like on * and WordPress. I can do this with the string type ( rails generate scaffold post title:string body:text tags:string ) and then comma separated, but it's not good practice since I want the reader to browse by tags ( e.g. /tags/web20 or /tags/lol ). How can I do this?

我正在使用Scaffolding在Rails中创建一个博客。我想在*和WordPress上的每个帖子上添加一个'tags'字段。我可以用字符串类型(rails生成脚手架帖子标题:字符串体:文本标签:字符串)然后以逗号分隔,但这不是好习惯,因为我希望读者按标签浏览(例如/ tags / web20或/标签/ lol)。我怎样才能做到这一点?

3 个解决方案

#1


17  

Tagging is so common that implementations are a commodity. I believe "acts as taggable on" is usually the preferred way of implementing tags.

标记很常见,实现是一种商品。我相信“可以作为标记”通常是实现标记的首选方式。

See other popular solutions here.

在此查看其他热门解决方案

If you wish to implement it yourself, you could dive into the source code to find some ideas.

如果您希望自己实现它,可以深入了解源代码以找到一些想法。

#2


1  

I would suggest creating a Tag model and using has_and_belongs_to_many to assign tags to posts. I don't know if the scaffold feature will help you create a form for that, but it shouldn't be difficult to add it yourself. I also suggest using the formtastic plugin as it's much easier and nicer to create forms with it.

我建议创建一个Tag模型,并使用has_and_belongs_to_many为帖子分配标签。我不知道脚手架功能是否会帮助您创建一个表单,但是自己添加它并不困难。我还建议使用formtastic插件,因为使用它创建表单更容易,更好。

#3


0  

Err, the usual way? Add Tag entity, add has_many :tags in your Post entity. Then migrate. That would be all.

呃,通常的方式?添加标记实体,在Post实体中添加has_many:标记。然后迁移。这就是全部。

#1


17  

Tagging is so common that implementations are a commodity. I believe "acts as taggable on" is usually the preferred way of implementing tags.

标记很常见,实现是一种商品。我相信“可以作为标记”通常是实现标记的首选方式。

See other popular solutions here.

在此查看其他热门解决方案

If you wish to implement it yourself, you could dive into the source code to find some ideas.

如果您希望自己实现它,可以深入了解源代码以找到一些想法。

#2


1  

I would suggest creating a Tag model and using has_and_belongs_to_many to assign tags to posts. I don't know if the scaffold feature will help you create a form for that, but it shouldn't be difficult to add it yourself. I also suggest using the formtastic plugin as it's much easier and nicer to create forms with it.

我建议创建一个Tag模型,并使用has_and_belongs_to_many为帖子分配标签。我不知道脚手架功能是否会帮助您创建一个表单,但是自己添加它并不困难。我还建议使用formtastic插件,因为使用它创建表单更容易,更好。

#3


0  

Err, the usual way? Add Tag entity, add has_many :tags in your Post entity. Then migrate. That would be all.

呃,通常的方式?添加标记实体,在Post实体中添加has_many:标记。然后迁移。这就是全部。