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

时间:2013-04-17 10:09:11

标签: ruby-on-rails ruby tags

我试图显示在标签下发布的所有文章(目前,标签显示在文章索引页面上)。我可以在文章索引页面上获取所有标签。但是这些标签没有任何文章。在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 %>

3 个答案:

答案 0 :(得分:1)

假设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

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我得到了解决方案。我和Sunxperous很相似。我这样实现: - &lt;%@ tags.each do | tag | %GT;            &lt;%= link_to tag.name,tag,:class =&gt; “tag”%&gt;          &lt;%end%&gt;。因为我已经在标签控制器中有索引和显示动作。在“show.html.erb of tag中,我确实喜欢这样 - :&lt;%@ tag.articles.each do | article |%&gt;     

  • &lt;%= link_to article.title,article_path(article)%&gt;