如果数据库条目为空,则显示此“消息”在视野中
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
或者,如果您知道
post.info
中没有任何内容时将为nil
(而不是空字符串):或者,如果您需要包含一些标记:
顺便说一句,此行为:
当没有要显示的图像时取决于浏览器:有些会给您一个损坏的图像图标,有些会显示
alt
文本,有些会不显示任何内容。您正在滥用alt
属性:alt
文本应该是适合屏幕阅读器和其他非图形界面的图像描述,因此使用当前没有可用的图像作为alt value 没有多大意义。
You could do this:
or, if you know that
post.info
will benil
(rather than an empty string) when there isn't anything in it:or, if you need to include some markup:
BTW, the behavior of this:
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 thealt
attribute: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 thealt
value doesn't make much sense.由于 mu 太短,答案基本相同,我将删除我的答案并给您另一种可能性。
如果这始终是关于空字符串或
nil
的对象,那么您可以向 Ruby 的String
和NilClass< 添加
or
方法/code>:有了这个你可以这样做:
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 anor
method to Ruby'sString
andNilClass
:With this you can do: