gpt4 book ai didi

ruby-on-rails - rails : Check has_many in View

转载 作者:数据小太阳 更新时间:2023-10-29 07:42:58 25 4
gpt4 key购买 nike

如果我有...

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 %>

关于ruby-on-rails - rails : Check has_many in View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/297435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com