【问题标题】:Showing the 20 first instances of this Tag model in an index view of this other Post model?在另一个 Post 模型的索引视图中显示这个 Tag 模型的 20 个第一个实例?
【发布时间】:2012-03-14 06:52:16
【问题描述】:

我有一个 PostTag 模型。

我想在 Post 模型的索引视图中显示 Tag 模型的前 20 个实例。

(与您在侧边栏中的 StackOverflow 主页中看到的差不多)。

最好的方法是什么?

(顺便说一句,我用的是will_paginate

编辑:

我尝试在帖子索引视图中添加render 'tags/index,在views/tags 中创建了一个index.html.erb 文件。

但我得到了这个:

显示 /home/alex/rails/r18/app/views/layouts/_sidebar.html.erb 在哪里 第 14 行提出:

缺少带有 {:locale=>[:en]、:formats=>[:html] 的部分标签/索引, :handlers=>[:erb, :builder, :coffee]}。搜索:* “/home/alex/rails/r18/app/views”* “/home/alex/.rvm/gems/ruby-1.9.2-p290/gems/ckeditor-3.7.0.rc3/app/views” * "/home/alex/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.4/app/views"

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    第一个还是最近的?

     # in your post index controller action
    @tags= Tag.limit(20).order("ID asc")
    
    # in your view 
    <% @tags.each do |tag| %>
      <%= tag.name %>
    <% end %>
    

    显示前 20 个标签。

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      相关资源
      最近更新 更多