Take the 2-minute tour ×

I am creating a blog in Rails using the Scaffold feature. I would like to add a 'tags' field to each post like on StackOverflow and Wordpress. I can do this with a string type ( scaffold post title:string body:text tags:string ) and then comment seperated but this isn't a good practice, since I also want the reader to be able to browse by tags ( e.g. /tags/web20 or /tags/lol ). How can I do this?


share|improve this question
I think you have to make something like scaffold tag tagname:string Each post can have several tags and each tag can have several posts. I haven't done this yet but I am interessted in doing this too! –  Newbie Jan 12 '10 at 15:45
add comment

3 Answers

up vote 15 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
I did that and migrated. I'm fairly new to has_many and belongs_to. Could you also explain me how I can implement this in my controller and view? Thanks! –  user142019 Jan 12 '10 at 15:51
Read molf's answer. –  F0RR Jan 12 '10 at 16:19
add comment

Your Answer

