如果数据库条目为空,则显示此“消息”在视野中

发布于 2024-12-25 12:36:23 字数 808 浏览 3 评论 0原文

我是 Rails 新手,想知道如果数据库条目为空,是否可以在视图中显示默认消息。例如,如果您有一张图像,您可以这样做:

<%= image_tag "#{post.imge}", :alt => 'Currently no image available'%>

因此,如果其中没有图像链接,则会显示“当前没有可用的图像” 但是,如果您只想显示一个字符串,那么:

<%= post.info %>

但是如果 post.info 为空,我想显示一条替代消息,例如“当前没有可用信息”?我知道 <%= post.info, :alt => '目前没有可用信息'%> 不起作用,但我似乎找不到任何方法来做到这一点 - 可能是因为它太简单了,没有人问过这个问题,哈哈!

我正在尝试在助手中使用下面的解决方案,因为我想用于几个不同的条目,但我似乎无法让它工作 - 我在下面添加了我的代码:

module ApplicationHelper
  def chk_blnk(gogo)
    if gogo.blank?
      "No img"
    else    
      return gogo
    end
  end
end

我也不确定如何调用它在视图中,当它接受变量(post.info)时,我知道通常您会这样称呼它:

<%= chk_blnk %>

我很抱歉,如果这些确实是基本问题,我已经尝试寻找答案,但我似乎再次找不到任何。

I am new to rails and was wondering if it were possible to have a default message in the view if the database entry was empty. For example if you had an image you could do this:

<%= image_tag "#{post.imge}", :alt => 'Currently no image available'%>

So if there wasn't an image link in there it would come up with "Currently no image available"
But say if you just wanted to show a string so:

<%= post.info %>

But if post.info were empty I want display an alternative message like "Currently no information available"? I know that
<%= post.info, :alt => 'Currently no information available'%>
doesn't work but I can't seem to find any way to do this - probably because it is so easy no one has ever asked the question lol!

I am trying to use the solution below in a helper since I want to use for a few different entries but I can't seem to get it to work - I have added my code below:

module ApplicationHelper
  def chk_blnk(gogo)
    if gogo.blank?
      "No img"
    else    
      return gogo
    end
  end
end

I'm also not sure how to call it in the view when it takes in a variable (post.info) I know that usually you would call it as such:

<%= chk_blnk %>

I'm sorry if these are really basic questions I have tried searching for answers but again I can't seem to find any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2025-01-01 12:36:23

您可以这样做:

<%= post.info.blank?? 'No post' : post.info %>

或者,如果您知道 post.info 中没有任何内容时将为 nil(而不是空字符串):

<%= post.info || 'No Post' %>

或者,如果您需要包含一些标记:

<% if post.info.blank? %>
    <p>No Post</p>
<% else %>
    <%= post.info %>
<% end %>

顺便说一句,此行为:

<%= image_tag "#{post.imge}", :alt => 'Currently no image available' %>

当没有要显示的图像时取决于浏览器:有些会给您一个损坏的图像图标,有些会显示 alt 文本,有些会不显示任何内容。您正在滥用alt 属性

标记 img 元素的替代文本的首选方法是使用 alt 属性为图像提供完整的等效项,或者提供 < code>alt 属性具有空值(表示图像纯粹是演示性的)。

alt 文本应该是适合屏幕阅读器和其他非图形界面的图像描述,因此使用当前没有可用的图像作为alt value 没有多大意义。

You could do this:

<%= post.info.blank?? 'No post' : post.info %>

or, if you know that post.info will be nil (rather than an empty string) when there isn't anything in it:

<%= post.info || 'No Post' %>

or, if you need to include some markup:

<% if post.info.blank? %>
    <p>No Post</p>
<% else %>
    <%= post.info %>
<% end %>

BTW, the behavior of this:

<%= image_tag "#{post.imge}", :alt => 'Currently no image available' %>

when there is no image to display is browser dependent: some will give you a broken image icon, some will display the alt text, some will display nothing. You are abusing the alt attribute:

The preferred means for marking up a text alternative for an img element is to use the alt attribute to provide a full equivalent for the image, or to provide an alt attribute with an empty value (to indicate that the image is purely presentational).

The alt text is supposed to be a description of the image suitable for screen readers and other non-graphic interfaces so using Currently no image available as the alt value doesn't make much sense.

歌入人心 2025-01-01 12:36:23

由于 mu 太短,答案基本相同,我将删除我的答案并给您另一种可能性。

如果这始终是关于空字符串或 nil 的对象,那么您可以向 Ruby 的 StringNilClass< 添加 or 方法/code>:

class String
  def or(default)
    self.empty? ? default : self
  end
end

class NilClass
  def or(default)
    default
  end
end

有了这个你可以这样做:

<%= post.info.or('Currently no information available') %>

Since mu is too short's answer's basically the same, I'll delete mine and give you one other possibility.

If this is always about empty strings or objects that are nil, then you could add an or method to Ruby's String and NilClass:

class String
  def or(default)
    self.empty? ? default : self
  end
end

class NilClass
  def or(default)
    default
  end
end

With this you can do:

<%= post.info.or('Currently no information available') %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文