Ruby on Rails中,如何在视图里验证has_many关联的存在性?

更新于
2026-10-03 19:27:12
0阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计192个文字,预计阅读时间需要1分钟。

如果我有... class Bunny ActiveRecord::Base has_many :carrots...

如果Bunny有胡萝卜,我如何查看视图?我想做这样的事情:

% if @bunny.carrots? Yay! Carrots! % for carrot in @bunny.carrots You got a % carrot.name % % end

如果我有…

class Bunny < ActiveRecord::Base has_many :carrots end

…如果@bunny有胡萝卜,我如何查看View?我想做这样的事情:

<% if @bunny.carrots? %> <strong>Yay! Carrots!</strong> <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!<br /> <% end %> <% end %>

我知道@ bunny.carrots?不起作用 – 会怎么样?

<% if @bunny.carrots.any? %> <strong>Yay! Carrots!</strong> <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!<br /> <% end %> <% end %>

本文共计192个文字,预计阅读时间需要1分钟。

如果我有... class Bunny ActiveRecord::Base has_many :carrots...

如果Bunny有胡萝卜,我如何查看视图?我想做这样的事情:

% if @bunny.carrots? Yay! Carrots! % for carrot in @bunny.carrots You got a % carrot.name % % end

如果我有…

class Bunny < ActiveRecord::Base has_many :carrots end

…如果@bunny有胡萝卜,我如何查看View?我想做这样的事情:

<% if @bunny.carrots? %> <strong>Yay! Carrots!</strong> <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!<br /> <% end %> <% end %>

我知道@ bunny.carrots?不起作用 – 会怎么样?

<% if @bunny.carrots.any? %> <strong>Yay! Carrots!</strong> <% for carrot in @bunny.carrots %> You got a <%=h carrot.color %> carrot!<br /> <% end %> <% end %>