简体   繁体   English

如何在Rails上显示在ruby标签下发布的所有文章

[英]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. 但是这些标签没有任何文章。在db中,标签和文章表之间存在关系,如下所示。 Please suggest me how to display all the articles under a tag on article index page. 请建议我如何在文章索引页面上的标签下显示所有文章。

articles_controller.rb 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 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 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) article / index.html.erb(我需要在这些文章下方显示标签的地方)

 <% if !@tags.nil? %>
           <% @tags.each  do | tags | %>

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

Assuming that the relationship between Article and Tag has been properly defined as has_many :through => :taggings , as such: 假设Article和Tag之间的关系已正确定义为has_many:through =>:taggings ,如下所示:

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: articles / index.html.erb中使用以下代码来显示属于每个标记的文章:

<% @tags.each do |tag| %>
  <%= link_to tag.name, tag %>
  <% tag.articles.each do |article| %>
    <%= link_to article.title, article %>
  <% end %>
<% end %>
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 现在,您可以通过@article.tags获得文章的所有标签,并通过@tag.articles获得标签中的所有文章

For additional info, refer guides.rubyonrails.org/association_basics.html 有关其他信息,请参阅guides.rubyonrails.org/association_basics.html

I got solution. 我有解决办法。 I is similar to Sunxperous. 我类似于Sunxperous。 I implemented like this:- <% @tags.each do |tag| 我是这样实现的:-<%@ tags.each做| tag | %> <%= link_to tag.name, tag, :class => "tag" %> <% end %>. %> <%= link_to tag.name,标记,: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| %> 因为我已经在标签控制器中有了索引并显示了动作。在“标签的show.html.erb中,我确实喜欢这样-:<%@ tag.articles.each | article |%>

  • <%= link_to article.title, article_path(article) %> <%=链接到article.title,article_path(文章)%>
  • 声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM