Search code examples
ruby-on-railsruby-on-rails-4acts-as-taggable-on

Why isn't my second tag type working?


ROR rookie. I'm using the acts_as_taggle_on gem for tagging, and tried to create two different types of tags, similar to this post. I've got the form working, and it adds two different tag types, but something must be wrong with my controller because only one of the tag types works (the regular tags — 'series' doesn't work). When I say 'doesn't work' I mean the tags on the show page are clickable links that filter my index page down to only those artworks that have the tag that I linked from: this works for 'tags' but not for 'series.' Series just shows me all the artworks, instead of only the ones with the series tag that I linked from.

Controller: not very DRY! suggestions welcome. :)

def index
    if params[:tag]
      @artworks = Artwork.tagged_with(params[:tag])
    else
      @artworks = Artwork.all
    end
  end

  def tagged
    if params[:tag].present? 
      @artworks = Artwork.tagged_with(params[:tag])
    else 
      @artworks = Artwork.all
    end  
  end

  def series
    if params[:series].present? 
      @artworks = Artwork.tagged_with(params[:series])
    else 
      @artworks = Artwork.all
    end  
  end

Strong params in control permits:

:tag_list, :tag, :series_list, :series

Model:

acts_as_taggable
acts_as_taggable_on :tags, :series

Routes:

get 'series' => 'artworks#index', :as => 'series'
get 'tagged' => 'artworks#index', :as => 'tagged'
root "artworks#index"

Show:

<% unless @artwork.series.blank? %>
  <div class="col-xs-6 col-sm-6 col-md-4">
    <p class="field-label">Series</p>
    <% @artwork.series.each do |series| %>
      <span class="tags">
        <%= link_to series.name, series_url(:series => series.name) %>
      </span>
    <% end %>
  </div>
<% end %>

<% unless @artwork.tags.blank? %>
  <div class="col-xs-6 col-sm-6 col-md-4">
    <p class="field-label">Tags</p>
    <% @artwork.tags.each do |tag| %>
      <span class="tags">
        <%= link_to tag.name, tagged_url(:tag => tag.name) %>
      </span>
    <% end %>
  </div>
<% end %>

Index:

<div id="artworks" class="transitions-enabled">
  <% @artworks.each do |artwork| %>
    <div class="box">
        <%= link_to image_tag(artwork.image.url(:medium)), artwork %>
    </div>
  <% end %>
</div>

Thanks in advance for your help!


Solution

  • Finally got this to work. Here's what I did in my controller that made things work:

    def index
        if params[:tag] 
          @artworks = Artwork.tagged_with(params[:tag])
        elsif params[:series]
          @artworks = Artwork.tagged_with(params[:series])
        else
          @artworks = Artwork.all
        end
      end
    
      def tagged
          @artworks = Artwork.tagged_with(params[:tag])
      end
    
      def series
          @artworks = Artwork.tagged_with(params[:series]) 
      end