如果栏位为空白,请勿显示栏位 [英] Dont show field if blank Rails
问题描述
在我的rails表中,我有一个text_field
其中my_website
,所以<%= f.text_field:my_website%>
In my rails table i have a text_field
which my_website
, so <%= f.text_field :my_website %>
等等,在show.html.erb
<a href="<%= @user.my_website %>">Go to my website</a>
效果很好
但是说用户没有以my_website的形式输入任何内容,如果用户不输入my_website,我将如何使其工作,以便隐藏这部分<a href="<%= @user.my_website %>">Go to my website</a>
but say the user doesnt input anything in the form for my_website, how would i make it work so that this part<a href="<%= @user.my_website %>">Go to my website</a>
hides if the user doesnt input my_website
基本上是这样的
if user puts in my_website, show
<a href="<%= @user.my_website %>">Go to my website</a>
else
不显示任何内容
推荐答案
我认为这是在表单提交之后.您可以只使用if
语句.
I assume this is after form submission. You can just use an if
statement.
<% if @user.my_website %>
<%= link_to "Go to my website", @user.my_website %>
<% end %>
或者,如果用户为my_website
字段输入了一堆空格,则下面的解决方案将不会显示"Go to my website"
.如果@ user.my_website为nil或包含空字符串,则blank?
将返回true.
Alternatively, the solution below will not display "Go to my website"
if the user inputs a bunch of whitespaces for the my_website
field. blank?
will return true if @user.my_website is nil or contains an empty string.
<% unless @user.my_website.blank? %>
<%= link_to "Go to my website", @user.my_website %>
<% end %>
这篇关于如果栏位为空白,请勿显示栏位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!