Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 on rails Rails 4如何在列表中显示要记录的内容_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails Rails 4如何在列表中显示要记录的内容

Ruby on rails Rails 4如何在列表中显示要记录的内容,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,所以请容忍我:) group.rb class Group < ActiveRecord::Base def to_s name end 类组true,:university=>{:区分大小写=>false} 验证:content,:presence=>true 结束 category.rb class Group < ActiveRecord::Base def to_s name end 类别true,:university=>{:区分大小写

所以请容忍我:)

group.rb

class Group < ActiveRecord::Base
  def to_s
    name
  end
类组true,:university=>{:区分大小写=>false}
验证:content,:presence=>true
结束
category.rb

class Group < ActiveRecord::Base
  def to_s
    name
  end
类别true,:university=>{:区分大小写=>false}
验证:content,:presence=>true
验证:group_id,:presence=>true
结束
app/views/categories/index.html.erb

class Group < ActiveRecord::Base
  def to_s
    name
  end


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

很简单,只要写下:

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


一个好主意是为类定义函数“to_s”。 比如说

group.rb

class Group < ActiveRecord::Base
  def to_s
    name
  end
类组
这样,Rails将在打印组时自动输出组的名称,这意味着

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



将输出相同的信息。

使用
显示属于此类别的组。

我以前尝试过,这是我的控制器代码:@categories=Category.all。但不起作用抱歉,现在起作用了,我忘了将组id添加到强参数中,谢谢您的帮助:)
<td><%= category.group %></td>
<td><%= category.group.name %></td>