【问题标题】:how to display all articles published under a tag in ruby on rails如何在 ruby​​ on rails 中显示在标签下发表的所有文章
【发布时间】:2013-04-17 10:09:10
【问题描述】:

我正在尝试显示在标签下发布的所有文章(目前,标签显示在文章索引页面上)。我可以获取文章索引页面上的所有标签。但是这些标签没有任何文章。在db中,标签和文章表之间存在关系,下面发布。请建议我如何在文章索引页面上显示标签下的所有文章。

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(我需要在这些文章下显示标签)

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

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

【问题讨论】:

    标签: ruby-on-rails ruby tags


    【解决方案1】:

    假设文章和标签之间的关系已正确定义为has_many :through => :taggings,如下所示:

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

    并且结果的格式应如以下示例所示:

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

    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 %>
    

    【讨论】:

    • 您好 sunxperous,感谢您的回复。你的建议几乎是我正在寻找的。正是我想在文章索引页面上显示数据库中的所有标签,当单击任何标签时,它将显示在其下发布的所有文章。我不想在您建议的标签下显示文章。任何建议都会被采纳。
    • 嗨 Sunxperous,我想要这样 - Tag1、Tag2、Tag3,当点击这些标签中的任何一个时,将向我们显示在它们下发布的文章。
    • 为了在 articles/index 中实现这一点,您需要使用 Ajax 或 Javascript。没有它,使用 TagsController 中的 show 操作来实现你想要的会更容易,即 /tags/1 显示 Tag1 的文章, /tags/2 显示 Tag2 的文章。
    【解决方案2】:
    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
    

    现在您可以通过@article.tags获取文章的所有标签,通过@tag.articles获取标签中的所有文章

    有关更多信息,请参阅 guides.rubyonrails.org/association_basics.html

    【讨论】:

    • 嗨 manoj,感谢您的重播。当我尝试 ,我在 Articles#index 中得到 NoMethodError 任何建议。显示 /Users/harshaldhumal/Desktop/kuta/billi/app/views/articles/index.htm 未定义的方法 `articles' 用于 nil:NilClass 错误。
    【解决方案3】:

    我得到了解决方案。我类似于 Sunxperous。我是这样实现的:- "tag" %> 。因为我已经在标签控制器中有索引和显示操作。在标签的“show.html.erb”中,我确实喜欢这样-:

  • 【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多