I’m creating a discussion forum. Instead of having a nested forum structure categories, I want each discussion thread to have ‘tags’. I envision it being the same as the Stackoverflow structure where you can see the questions by tag and then further filter the questions by adding additional tags.
For example:
https://stackoverflow.com/questions/tagged/ruby-on-rails
Can be further filtered with: https://stackoverflow.com/questions/tagged/ruby-on-rails+ruby
And so on…
My question is how to do this in ROR. I have a model Forum and ForumTag linked with a HABTM relationship.
My routes are:
resources :forums do
resources :forum_posts, :only => [:new, :edit, :create, :destroy]
match 'tagged/:name' => 'forums#tag', :as => 'forums_by_tag', :on => :collection
end
In my forums_controller #tag:
@forums = Forum.where(:forum_tags => {:name => params[:name]}).includes(:forum_tags).all
At this point, I get stuck as I don’t know how to bring in multiple tag names. Furthermore, I don’t know how to: a) access all the forum_tags that exist in all the @forums and b) to then make links that take the current path and add ‘+new_tag_name’ to it.
(a) @tags = @forums.forum_tags #doesn't work
(b) <%= link_to tagname, forums_by_tag_forum_path(params[:name]+'+'+tagname) %> #doesn't look right
Any suggestions?
You should try this plugin, it has everything you need and works very smoothly:
https://github.com/mbleigh/acts-as-taggable-on