Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 包含嵌套资源的列表_Ruby_Ruby On Rails 3_Nested - Fatal编程技术网

Ruby 包含嵌套资源的列表

Ruby 包含嵌套资源的列表,ruby,ruby-on-rails-3,nested,Ruby,Ruby On Rails 3,Nested,我有一个Rails 3.2.1应用程序,其中: class Article < ActiveRecord::Base belongs_to :category class Category < ActiveRecord::Base has_many :articles 我想在我的文章中显示#为所有类别及其文章列表编制索引。 在我的文章中: def index #Categories @categories = Category.all @category =

我有一个Rails 3.2.1应用程序,其中:

  class Article < ActiveRecord::Base
   belongs_to :category

  class Category < ActiveRecord::Base
   has_many :articles
我想在我的文章中显示#为所有类别及其文章列表编制索引。 在我的文章中:

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
我的观点(文章索引)


问题是它列出了所有类别,但似乎不尊重category_id参数。
在每个类别中,它显示所有的文章,而不是每个类别的文章。

只有在没有设置正确的
params[:category\u id]
的情况下才有问题?因为当没有@feature_article和@category_articles这样的参数时,第一件奇怪的事情是,如果没有设置@category,您将得到每个类别的所有文章(但可能您需要这个)。尝试访问控制器中的
p参数[:category_id]
,并将其输出到日志中,然后检查日志。抱歉回答得太晚,我想我必须先找到类别,然后显示该类别的文章,但我在文章索引中列出了12个类别。我会试试你的建议,谢谢!
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
 <% @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 %>