0

私はRails 3.2.1アプリケーションを持っています:

  class Article < ActiveRecord::Base
   belongs_to :category

  class Category < ActiveRecord::Base
   has_many :articles

ルート内:

   resources :categories, :shallow => true do
      resources :articles
   end

Articles#index にすべてのカテゴリとその記事のリストを表示したいと考えています。私の記事のコントローラーでは:

def index

#Categories
@categories = Category.all
@category = Category.find(params[:category_id]) if params[:category_id].present?
@category_articles = @category.present? ? @category.articles : Article.where("created_at >= ?", Time.now - 3.days).order("views DESC", :limit => 5).offset(1)
@feature_article = @category.present? ? @category.articles : Article.where("created_at >= ?", Time.now - 3.days).order("views DESC").first
end

私の見解 (articles#index)

 <% @categories.each do |category| %>
<%= link_to category.name, category %> <!-- category name -->
    <%= link_to @feature_article.title, @feature_article %> <!-- feature article -->
    <% @category_articles.each do |article| %> <!-- Each category articles -->
              <%= link_to article.title, article %>
    <% end %>
 <% end %>

問題は、すべてのカテゴリをリストすることですが、category_id パラメータを尊重していないようです。各カテゴリでは、各カテゴリの記事ではなく、すべての記事が表示されます。

4

0 に答える 0