Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1acts-as-taggable-on

show custom content based on tag - acts-as-taggable-on


I just implement acts-as-taggable-on on my blog model. Working great.

The visitor can filter blogpost based on the tag. So on my index blog there is a tagcloud with a bunch of tags (links). The url is domain.com/tag/greatbars ect

Question: How can i place some unique content on top of my index based on the filtered tag?

example: blogpost are filter by "greatbars" h1: The posts are filter by "tagname" h2: Below you see some great bars in the region of amsterdam. Check it out. p: ect ect

Blog listing based on filtered tag(s)


Solution

  • The action which renders the index presumably takes the filtered tag as a parameter. You can save that tag as an instance variable, and then in the index view display certain content if that variable has a particular value. Basic example:

    Controller

      def index
        @tag = Tag.find_by_name(params[:tag])
        @posts = Post.tagged_with(@tag)
      end
    

    View

      <% case @tag.name %>
      <% when 'bars' %>
        <%= render :partial => 'posts/bars')
      <% when 'foos' %>
        <%= render :partial => 'posts/foo_stuff')
      # as many other cases as you wish
      <% else %>
        <%= render :partial => 'posts/default')
      <% end %>
    # rest of existing index view