显示用户可能没有的数据的最佳做法

时间:2021-01-15 16:33:48

I've got a Ruby on Rails app where I do the following:

我有一个Ruby on Rails应用程序,我在其中执行以下操作:

@user = User.find(:first, :conditions => ['LOWER(username) = ?', current_subdomain.downcase], :include => :bio)

Where ':include => :bio' is the important part.

其中':include =>:bio'是重要的部分。

Then in the view I want to display some bio:

然后在视图中我想显示一些生物:

<em><%= @user.bio.title %></em><br />
<%= @user.bio.city %><br />
<%= @user.bio.state %><br />
<%= @user.bio.country %>

But if the user dosn't have any information it just will turn up a couple of blank rows.

但是如果用户没有任何信息,它只会出现几行空白。

I've tried a couple of different things, none has worked so far...

我尝试了几件不同的东西,到目前为止还没有工作......

<% if @user.bio.title %> # is always true
<% if @user.bio.title > 0 %> # triggers an error if @user.bio.title isn't set
<% unless @user.bio.title.empty? %> # same as above

Any sollution to how to just display the data the user has got?

如何只显示用户获得的数据?

Thanks in advance!

提前致谢!

UPDATE

Ok, if figured some things out:

好吧,如果弄清楚了一些事情:

<% if @user.bio.title %> # Works if the bio isn't set at all.
<% if @user.bio.title > 0 %> # Works if the bio is set.

So I can use a sollution that looks like:

所以我可以使用看起来像这样的溶剂:

<% if @user.bio.title %><% if @user.bio.title > '0' %><%= @user.bio.title %><% end %><% end %>

But it seam a little overkill? Any better suggestions?

但它缝了一点矫枉过正?还有更好的建议?

Thanks!

3 个解决方案

#1


3  

Here's one way to think about it. The user has a bio and has filled in the title field. In Ruby:

这是思考它的一种方式。用户有一个生物,并填写了标题字段。在Ruby中:

<% if @user.bio && !@user.bio.blank? %> 

If you are going to display multiple fields from the bio you can separate this into 2 checks e.g.

如果您要显示bio中的多个字段,可以将其分为2个检查,例如

<% if @user.bio %>
  <% unless @user.bio.title.blank? %>
    Title: <%= @user.bio.title %>
  <% end %>
  <% unless @user.bio.other_field.blank? %>
    Other field: <%= @user.bio.other_field %>
  <% end %>
<% end %>

Alternative

Another way is to put methods on your model to provide direct access to the bio fields. e.g.

另一种方法是在模型上放置方法以提供对生物领域的直接访问。例如

# return the user's title or nil if no bio
def title
  bio.title if bio
end

Then in your view you can just do:

然后在您的视图中,您可以这样做:

<% unless @user.title.blank? %>
  Title: <%= @user.title %>
<% end %>

#2


0  

Can you try this?

你能试试吗?

if defined?(@user.bio.title)

#3


0  

Maybe you can check out object.try or andand?

也许你可以查看object.try或者和?

#1


3  

Here's one way to think about it. The user has a bio and has filled in the title field. In Ruby:

这是思考它的一种方式。用户有一个生物,并填写了标题字段。在Ruby中:

<% if @user.bio && !@user.bio.blank? %> 

If you are going to display multiple fields from the bio you can separate this into 2 checks e.g.

如果您要显示bio中的多个字段,可以将其分为2个检查,例如

<% if @user.bio %>
  <% unless @user.bio.title.blank? %>
    Title: <%= @user.bio.title %>
  <% end %>
  <% unless @user.bio.other_field.blank? %>
    Other field: <%= @user.bio.other_field %>
  <% end %>
<% end %>

Alternative

Another way is to put methods on your model to provide direct access to the bio fields. e.g.

另一种方法是在模型上放置方法以提供对生物领域的直接访问。例如

# return the user's title or nil if no bio
def title
  bio.title if bio
end

Then in your view you can just do:

然后在您的视图中,您可以这样做:

<% unless @user.title.blank? %>
  Title: <%= @user.title %>
<% end %>

#2


0  

Can you try this?

你能试试吗?

if defined?(@user.bio.title)

#3


0  

Maybe you can check out object.try or andand?

也许你可以查看object.try或者和?