简体   繁体   English

除非另加价值

[英]Rails unless puts another value

In a rails loop with many values, what's the best method of doing something like: 在具有许多值的rails循环中,执行类似以下操作的最佳方法是:

<% @results.each do |r| %>
<%= r.title, unless r.title.blank? puts 'N/A' %>
<%= r.year, unless r.year.blank? puts 'N/A' %>
<%= r.description, unless r.description.blank? puts 'N/A' %>
<% end %>
<%= r.title.presence || 'N/A' %>

等等...

I would probably do something like 我可能会做类似的事情

r.title.presence || 'N/A'

You should not use puts here. 您不应该在这里使用puts puts is for writing to stdout . puts用于写入stdout

Try with: 尝试:

<% @results.each do |r| %>
  <%= r.title.presence || 'N/A' %>
  <%= r.year.presence || 'N/A' %>
  <%= r.description.presence || 'N/A' %>
<% end %>

You may also want to have a look to the Draper gem . 您可能还想看看Draper宝石

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM