我正在尝试显示在标签下发布的所有文章(目前,标签显示在文章索引页面上)。我可以获取文章索引页面上的所有标签。但是这些标签没有任何文章。在db中,标签和文章表之间存在关系,下面发布。请建议我如何在文章索引页面上显示标签下的所有文章。
文章控制器.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
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
架构.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 "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
article/index.html.erb(我需要在这些文章下显示标签)
<% if !@tags.nil? %>
<% @tags.each do | tags | %>
<div style="margin-top:15px; margin-left:8px"> <%= link_to tags.name, "/path1" %></div>
<% end%>
<% end %>