views:

245

answers:

3

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?

Thanks

A: 

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

F0RR
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!
Time Machine
Read molf's answer.
F0RR
+3  A: 

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.

molf
+1  A: 

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.

Tomas Markauskas