繁体   English   中英

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

[英]displaying related articles according to tag in ruby on rails

我正在尝试在文章的显示页面上显示相关文章。 当用户查看任何特定文章时,db中的所有相关文章都应根据标签显示在该页面(右侧栏中)(因为每个文章至少具有一个标签)。我的应用程序具有标签和文章之间的关系(请在下面) 。

article_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

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

当前article / show.html.erb页面具有标签名称,我想在db中显示所有具有相同标签的关节,以显示在此页面上(右侧栏)。 任何想法如何实现此关系以获取相关文章,以及在何处编写此逻辑以及如何在视图中实现。

如果您使用的是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 })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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