displaying related articles according to tag in ruby on rails -
i trying show related articles on show page of article. when user views particular article, related articles in db should displayed on page ( in right side bar) according tag( every article has at-least 1 tag).my app has relation between tag , article (please below).
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) create_table "articles", :force => true |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 |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 |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 |t| t.string "name" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end
articles/show.html.erb :- need display related articles when user views particular article based on tag
currently articles/show.html.erb page has tag(s) name , want display artilces same tag in db display on page (right side bar). idea how implement relation fetch out related articles , write logic , how implement in views.
if using acts_as_taggable_on, want easy achieve using following code
def show @article = article.find(params[:id]) @related_articles = article.tagged_with(@article.tag_list, any: true) end
since rolled own implementation, you'd have manually fetch.
first list of tags of article. @article.tag_ids
.
next, list of articles associated tag ids
@related_articles = article .joins(:taggings) .where('articles.id != ?', @article.id) .where(taggings: { tag_id: @article.tag_ids })
Comments
Post a Comment