Rails 4如何显示belongs_to记录在列表中

猴子D.路飞

所以请忍受我:)

group.rb

class Group < ActiveRecord::Base
  has_many :categories, dependent: :destroy

  validates :name, :presence => true, :uniqueness => { :case_sensitive => false }
  validates :content, :presence => true
end

category.rb

class Category < ActiveRecord::Base
  belongs_to :group
  validates :name, :presence => true, :uniqueness => { :case_sensitive => false }
  validates :content, :presence => true
  validates :group_id, :presence => true
end

app / views / categories / index.html.erb

<% @categories.each do |category| %>
  <tr>
    <td><%= category.id %></td>
    <td>
      <%= link_to category.name, admin_category_path(category) %>
    </td>
    <td><%= ***[GROUP WHERE CURRENT CATEGORY BELONGS TO]*** %></td>
    <td>
      <%= link_to admin_category_path(category), class: "btn btn-info btn-xs" do %>
        <i class="fa fa-search"></i>
      <% end %>
      <%= link_to edit_admin_category_path(category), class: "btn btn-primary btn-xs" do %>
        <i class="fa fa-pencil"></i>
      <% end %>
      <%= link_to admin_category_path(category), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger btn-xs" do %>
        <i class="fa fa-trash-o"></i>
      <% end %>
    </td>
  </tr>
<% end %>

我的问题是:如何显示属于这个类别的组?

格恩贝格

很简单,只需编写:

<td><%= category.group %></td>

或者

<td><%= category.group.name %></td>

一个好主意是为您的类定义函数“ to_s”。例如

group.rb

class Group < ActiveRecord::Base
  def to_s
    name
  end

这样,Rails在打印组时将自动输出组的名称,这意味着

<td><%= category.group %></td>

<td><%= category.group.name %></td>

将输出相同的信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails 4如何显示belongs_to记录在列表中

来自分类Dev

无法创建或更新Rails 4中的belongs_to记录

来自分类Dev

Rails 4中的has_many和belongs_to关联

来自分类Dev

在Rails 4中以编程方式获取belongs_to关联的类

来自分类Dev

在Rails 4中检查现有的关联'belongs_to'

来自分类Dev

如何在Rails中通过HABTM查询belongs_to

来自分类Dev

在Rails中链接belongs_to关系

来自分类Dev

如何使用has_many / belongs_to使Rails 4关联自动保存到两个表中?

来自分类Dev

Rails 5 检查belongs_to 关系中是否存在记录?

来自分类Dev

Rails 4:在显示页面上确定has_many / belongs_to关系

来自分类Dev

rails在索引页面中显示记录列表

来自分类Dev

无法在关联模型的Active Admin索引属性中显示(belongs_to / has_many)-Rails 3.2

来自分类Dev

如何在Ruby on Rails 5.2中的belongs_to声明中为多态关系创建别名?

来自分类Dev

Rails查询更深的belongs_to

来自分类Dev

Programatically get the class of the belongs_to association in Rails 4

来自分类Dev

Rails 4嵌套表单,belongs_to保存问题

来自分类Dev

Rails 4渴望加载has_one和belongs_to

来自分类Dev

Rails 4渴望加载has_one和belongs_to

来自分类Dev

的Rails 4-设置belongs_to关系不起作用

来自分类Dev

如何在Rails模型的Rspec测试中禁用belongs_to:touch选项?

来自分类Dev

如何在Rails::ActiveRecord 中设置多个belongs_to 和has_many 关系?

来自分类Dev

Rails Has_many和Belongs_to中的模型关联

来自分类Dev

禁止某些参数记录在Ruby on Rails中

来自分类Dev

Rails:Rails Engine中的Belongs_to和Has_many关联

来自分类Dev

Rails:获取belongs_to class_name

来自分类Dev

Rails多态has_many / belongs_to

来自分类Dev

Rails:has_many和belongs_to

来自分类Dev

Rails的ActiveRecord的belongs_to关联未加载

来自分类Dev

rails has_many和belongs_To

Related 相关文章

  1. 1

    Rails 4如何显示belongs_to记录在列表中

  2. 2

    无法创建或更新Rails 4中的belongs_to记录

  3. 3

    Rails 4中的has_many和belongs_to关联

  4. 4

    在Rails 4中以编程方式获取belongs_to关联的类

  5. 5

    在Rails 4中检查现有的关联'belongs_to'

  6. 6

    如何在Rails中通过HABTM查询belongs_to

  7. 7

    在Rails中链接belongs_to关系

  8. 8

    如何使用has_many / belongs_to使Rails 4关联自动保存到两个表中?

  9. 9

    Rails 5 检查belongs_to 关系中是否存在记录?

  10. 10

    Rails 4:在显示页面上确定has_many / belongs_to关系

  11. 11

    rails在索引页面中显示记录列表

  12. 12

    无法在关联模型的Active Admin索引属性中显示(belongs_to / has_many)-Rails 3.2

  13. 13

    如何在Ruby on Rails 5.2中的belongs_to声明中为多态关系创建别名?

  14. 14

    Rails查询更深的belongs_to

  15. 15

    Programatically get the class of the belongs_to association in Rails 4

  16. 16

    Rails 4嵌套表单,belongs_to保存问题

  17. 17

    Rails 4渴望加载has_one和belongs_to

  18. 18

    Rails 4渴望加载has_one和belongs_to

  19. 19

    的Rails 4-设置belongs_to关系不起作用

  20. 20

    如何在Rails模型的Rspec测试中禁用belongs_to:touch选项?

  21. 21

    如何在Rails::ActiveRecord 中设置多个belongs_to 和has_many 关系?

  22. 22

    Rails Has_many和Belongs_to中的模型关联

  23. 23

    禁止某些参数记录在Ruby on Rails中

  24. 24

    Rails:Rails Engine中的Belongs_to和Has_many关联

  25. 25

    Rails:获取belongs_to class_name

  26. 26

    Rails多态has_many / belongs_to

  27. 27

    Rails:has_many和belongs_to

  28. 28

    Rails的ActiveRecord的belongs_to关联未加载

  29. 29

    rails has_many和belongs_To

热门标签

归档