根据铁轨上的红宝石标签显示相关文章

时间:2013-04-18 11:33:46

标签: 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

    def create
      @article = Article.new(params[:article])
      @article.user_id = current_user.id

      if @article.save
        flash[:success] = "article created!"
        redirect_to article_path(@article)

      else
        render 'new' 
      end 
    end

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

      redirect_to action:  'index'  
    end

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

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

      if @article.update_attributes(params[:article])
       flash.notice = "Article '#{@article.title}' Updated!"
       redirect_to article_path(@article)

      else 
        render 'edit'
      end
  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 "comments", :force => true do |t|
    t.text     "content"
    t.integer  "user_id"
    t.string   "article_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  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
  end

articles / show.html.erb: - 当用户根据标签查看任何特定文章时,我需要显示相关文章

目前articles / show.html.erb页面有标签(S)名称,我想显示db中具有相同标签的所有艺术品以显示在此页面上(右侧栏)。知道如何实现这种关系以获取相关文章以及在何处编写此逻辑以及如何在视图中实现。

1 个答案:

答案 0 :(得分:7)

如果您使用acts_as_taggable_on,使用以下代码

可以轻松实现您想要的效果
def show
  @article = Article.find(params[:id])
  @related_articles = Article.tagged_with(@article.tag_list, any: true)
end

由于您汇总了自己的实现,因此您必须手动执行提取。

首先获取文章所有标签的列表。那将是@article.tag_ids

接下来,获取与这些标记ID相关联的文章列表

@related_articles = Article
  .joins(:taggings)
  .where('articles.id != ?', @article.id)
  .where(taggings: { tag_id: @article.tag_ids })