기사의 show 페이지에 관련 기사를 표시하려고합니다. 사용자가 특정 기사를 볼 때 db의 모든 관련 기사는 태그에 따라 해당 페이지 (모든 기사에 적어도 하나의 태그가 있음)에 표시되어야합니다. 내 응용 프로그램은 태그와 기사 사이의 관계가 있습니다 (아래 참조하십시오) .ruby on rails의 태그에 따라 관련 기사를 표시합니다.
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) 이름이 있으며이 페이지 (오른쪽 사이드 바)에 표시 할 db에 동일한 태그가있는 모든 기사를 표시하려고합니다. 이 관계를 구현하여 관련 기사를 가져 오는 방법과이 논리를 작성하는 방법 및보기에서 구현하는 방법 당신이 원하는
안녕 jvnill, 감사 관련 문서의 목록을 얻을. 하지만 "acts_as_taggable_on"플러그인을 사용하지 않았습니다. 어떤 생각이 플러그인을 사용하여 그것을 달성하는 방법. 나는 당신이 질문에서 볼 수 있듯이 기사와 태그 테이블 사이의 DB에 협회가 있습니다. 어떤 도움이라도 감사 할 것입니다. –
업데이트 된 답변을 사용해 볼 수 있습니까? – jvnill
안녕하세요 jvnill, 귀하의 제안에 감사드립니다. 하지만 어떻게 뷰에서 "@related_articles"를 사용할 수 있습니까 (articles/show.html.erb). 어떠한 제안. –