Vieenay Siingh
Vieenay Siingh

Reputation: 867

how to display all articles published under a tag in ruby on rails

I am trying to show all articles published under a tag( currently, tags are displaying on article index page). I can fetch all tags on article index page. But those tags are not have any article with them.In db, there is relation between tag and article table which are posted below. Please suggest me how to display all the articles under a tag on article index page.

articles_controller.rb

class ArticlesController < ApplicationController
  before_filter :is_user_admin, only: [:new, :create, :edit, :destroy]

    def is_user_admin
      redirect_to(action: :index) unless current_user.try(:is_admin?) 
      return false 
    end

      def index
          @articles = Article.all(:order => "created_at DESC")
      @article_titles = Article.first(10)
      @tags = Tag.all
      end

    def show
      @article = Article.find(params[:id])
    end

      def new
      @article = Article.new
      end
    end

tags_controller.rb

class TagsController < ApplicationController
 #before_filter :user_signed_in, only: [:destroy]

   def index
    @tags = Tag.all
   end

    def show
     @tag = Tag.find(params[:id])
    end
end

schema.rb

ActiveRecord::Schema.define(:version => 20130411074056) do

  create_table "articles", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "user_id"
  end

  create_table "taggings", :force => true do |t|
    t.integer  "tag_id"
    t.integer  "article_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "taggings", ["article_id"], :name => "index_taggings_on_article_id"
  add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"

  create_table "tags", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false

article/index.html.erb(where I need to display tags and under those articles)

 <% if [email protected]? %>
           <% @tags.each  do | tags | %>

           <div style="margin-top:15px; margin-left:8px"> <%= link_to tags.name, "/path1"  %></div>
           <% end%>
           <% end %>

Upvotes: 1

Views: 2555

Answers (3)

Vieenay Siingh
Vieenay Siingh

Reputation: 867

I got solution. I is similar to Sunxperous. I implemented like this:- <% @tags.each do |tag| %> <%= link_to tag.name, tag, :class => "tag" %> <% end %>. As I already have index and show action in tag controller .In "show.html.erb of tag, I did like this-: <% @tag.articles.each do |article| %>

  • <%= link_to article.title, article_path(article) %>
  • Upvotes: 0

    manoj
    manoj

    Reputation: 1675

    class Article 
      has_many :taggings
      has_many :tags, :through => :taggings
      ....
    end
    class Taggings
      belongs_to :articles
      belongs_to :tags
      ...
    end
    class Tags
      has_many :taggings
      has_many :articles, :through => :taggings
      ...
    end
    

    Now you can get all the tags of the article by @article.tags and all articles in a tag by @tag.articles

    For additional info, refer guides.rubyonrails.org/association_basics.html

    Upvotes: 0

    Sun
    Sun

    Reputation: 777

    Assuming that the relationship between Article and Tag has been properly defined as has_many :through => :taggings, as such:

    class Article < ActiveRecord::Base
      has_many :tags, :through => :taggings
    end
    
    class Tag < ActiveRecord::Base
      has_many :articles, :through => :taggings
    end
    

    and that the result should be formatted as shown in the following sample:

    Tag 1
      Article 1
      Article 2
      Article 3
    Tag 2
      Article 2
      Article 4
    

    Use the following code in articles/index.html.erb to display the articles which belong to each tag:

    <% @tags.each do |tag| %>
      <%= link_to tag.name, tag %>
      <% tag.articles.each do |article| %>
        <%= link_to article.title, article %>
      <% end %>
    <% end %>
    

    Upvotes: 1

    Related Questions