2013-04-18 44 views
1

我想在文章的顯示頁面上顯示相關文章。當用戶查看任何特定的文章時,應該在該頁面(右側欄)中根據標籤(每篇文章至少有一個標籤)在db中顯示所有相關文章。我的app具有標籤和文章之間的關係(請在下面) 。根據軌道上的紅寶石標籤顯示相關文章

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 

文章/ show.html.erb: - ,我需要顯示相關文章當用戶根據標籤查看任何特定物品時

當前articles/show.html.erb頁面有標籤(S)名稱,我想顯示所有具有相同標籤的artilces在db中以顯示在此頁面上(右側欄)。任何想法如何實現這個關係來獲取相關文章,在哪裏寫這個邏輯以及如何在視圖中實現。

回答

5

如果您正在使用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 }) 
+0

喜jvnill,感謝您的答覆文章列表。但我沒有使用「acts_as_taggable_on」插件。任何想法如何實現它與這個插件。我有關聯的文章和標籤表中的數據庫,你可以看到有問題。任何幫助將升值。 –

+0

你可以嘗試更新的答案嗎? – jvnill

+0

嗨jvnill,謝謝你的建議。但我怎麼能在視圖(articles/show.html.erb)中使用「@related_articles」。任何建議。 –