【发布时间】:2016-01-25 06:30:07
【问题描述】:
我正在尝试显示一个列出文章类别的药丸表,如果选中,将仅在手风琴样式列表中显示该类别的文章。每篇文章可以有多个类别。
我的模型如下:
class Article < ActiveRecord::Base
belongs_to :user
has_many :article_categories
has_many :categories, through: :article_categories
validates :title, presence: true, length: {minimum: 3, maximum: 50}
validates :description, presence: true, length: {minimum: 10, maximum: 300}
validates :user_id, presence: true
end
class Category < ActiveRecord::Base
has_many :article_categories
has_many :articles, through: :article_categories
validates :name, presence: true, length: { minimum: 3, maximum: 25}
validates_uniqueness_of :name
end
class ArticleCategory < ActiveRecord::Base
belongs_to :article
belongs_to :category
end
我的文章控制器
class ArticlesController < ApplicationController
before_action :set_article, only: [:edit, :update, :show, :destroy]
before_action :require_user, except: [:index, :show,]
before_action :require_same_user, only: [:edit, :update, :destroy]
def index
@articles = Article.paginate(page: params[:page], per_page: 15)
@article_list = @articles.group_by { |t| t.categories.name }
@categories = Category.all
end
我的文章 index.html.erb 是
<h1 align="center">Listing all articles by Category</h1>
<div class="container">
<div align="center">
<h2 align="center">Find your articles below</h2>
<ul class="col-md-offset-3 nav nav-pills">
<% @categories.each do |category| %>
<li><a data-toggle="pill" href="#<%= category.name %>"><%= category.name %></a></li>
<% end %>
</ul>
</div>
<% @article_list.each do |category, article_items| %>
<div class="tab-content">
<div id="<%= category %>" class "tab-pane fade in">
<div class="panel-group" id="accordion">
<% article_items.each do |article_item| %>
<div class="panel panel-default">
<div class="panel-heading" >
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#<%= article_item.title %>">
<%= article_item.title %></a>
</h4>
</div>
<div id="<%= article_item.title %>" class="panel-collapse collapse">
<div class="panel-body"><%=article_item.description %></div>
</div>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
这会同时显示药丸和手风琴风格,但不会链接在一起。如何确保类别药丸选项卡仅显示手风琴中的相关文章?
【问题讨论】:
-
你的意思是点击一个类别药丸应该在手风琴中打开相应的标签?顺便说一句,不要使用
<a href ...>,而是使用link_to。 -
点击类别药丸将显示相关文章的手风琴,好的,谢谢
标签: ruby-on-rails ruby categories