0

I am new to ruby on rails (and programming) and this is probably a really stupid question. I am using Rails 3.2 and trying to use acts_as_taggable_on to generate tags on articles and to have those tags show on article index and show pages as a clickable links.

I have tags clickable on both the article show and index pages, but the links just go back to the index page and don't sort according to the tag name. I have scoured the Internet and pieced together the code below from various sources, but I am clearly missing something.

Any help is greatly appreciated, as I have exhausted my seemingly limited knowledge! Thanks.

Here is what I have:

class ArticlesController < ApplicationController
    def tagged
        @articles = Article.all(:order => 'created_at DESC')
        @tags = Article.tag_counts_on(:tags)
        @tagged_articles = Article.tagged_with(params[:tags])  
        respond_to do |format|
          format.html # index.html.erb
          format.json { render :json => @articles }
        end 
      end

     def index
        @articles = Article.paginate :page => params[:page], :per_page => 3    
        @tags = Article.tag_counts_on(:tags)
        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @articles }
        end
      end

module ArticlesHelper
  include ActsAsTaggableOn::TagsHelper
end

class Article < ActiveRecord::Base
  acts_as_ordered_taggable
  acts_as_ordered_taggable_on :tags, :location, :about 
  attr_accessible :tag_list
  scope :by_join_date, order("created_at DESC")
end

article/index.html.erb
<% tag_cloud(@tags, %w(tag1 tag2 tag3 tag4)) do |tag| %>
<%= link_to tag.name, articles_path(:id => tag.name) %>
<% end %>

article/show.html.erb
<%= raw @article.tags.map { |tag| link_to tag.name, articles_path(:tag_id => tag) }.join(" | ") %>

routes.rb file snippet

  authenticated :user do
    root :to => 'home#index'
  end

  devise_for :users
    resources :users, :only => [:show, :index]

  resources :images
  resources :articles

1 Answers1

0

You can run 'rake routes' from the terminal to see all your paths. Here your tags are pointing at articles_path, which you will see routes to the index action in the articles controller ("articles#index")

You could create another route in your routes.rb file, something like:

match 'articles/tags' => 'articles#tagged', :as => :tagged

Place it above others in the routes file if you want it to take precedence, and remember you can always run 'rake routes' in the terminal to see how the routes are interpreted.

see http://guides.rubyonrails.org/routing.html#naming-routes for more info (maybe read the whole thing)

Another (probably better) option would be to combine your desired functionality into the index action using params, e.g. .../articles?tagged=true. Then you could use logic to define the @articles variable in the index controller based on params[:tagged]. A simple example might be

 def index
    if params[:tagged]
      @articles = Article.all(:order => 'created_at DESC')
    else
      Article.paginate :page => params[:page], :per_page => 3
    end

    @tags = Article.tag_counts_on(:tags)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @articles }
    end
  end

This is called DRYing up your code (for Don't Repeat Yourself); it would save you the need for code duplication in the articles#tagged action, which would make it easier to understand and maintain your code base.

Hope that helps.

Tyler
  • 11,272
  • 9
  • 65
  • 105
  • Thanks for the comments. Things are still not working but I am reading up on routes and how to write proper methods. Hopefully I can get this together soon. Cheers. – Schipperius Jul 17 '12 at 01:59