Ruby Don';t在rails4中不显示空字段
名称: 性别: 年龄: 地址:Ruby Don';t在rails4中不显示空字段,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,名称: 性别: 年龄: 地址: 这段代码运行良好。它没有显示空字段,但我想知道还有其他方法可以这样做。因为在这里,我一次又一次地重复相同类型的代码。我可以使用任何停止显示空字段的助手吗?有很多方法可以做到这一点,最好的方法取决于您的情况。只要标签始终与属性相同,一种方法就是使用简单的局部视图: <p> <% if @person.name %> <strong>Name:</strong> <%= @per
这段代码运行良好。它没有显示空字段,但我想知道还有其他方法可以这样做。因为在这里,我一次又一次地重复相同类型的代码。我可以使用任何停止显示空字段的
助手吗?有很多方法可以做到这一点,最好的方法取决于您的情况。只要标签始终与属性相同,一种方法就是使用简单的局部视图:
<p>
<% if @person.name %>
<strong>Name:</strong>
<%= @person.name %>
<% end %>
</p>
<p>
<% if @person.gender %>
<strong>Gender:</strong>
<%= @person.gender %>
<% end %>
</p>
<p>
<% unless @person.age.blank? %>
<strong>Age:</strong>
<%= @person.age %>
<% end %>
</p>
<p>
<% unless @person.address.blank? %>
<strong>Address:</strong>
<%= @person.address %>
<% end %>
</p>
#person/_attribute.html.erb
<% if @person.public_send attribute != nil %>
<strong><%= attribute.to_s.capitalize %></strong>
<%= @person.public_send attribute %>
<% end %>
<p>
<%= render 'attribute' :attribute => :name %>
</p>
<p>
<%= render 'attribute' :attribute => :gender %>
</p>
<p>
<%= render 'attribute' :attribute => :age %>
</p>
<p>
<%= render 'attribute' :attribute => :address %>
</p>
#person/_attribute.haml
- if @person.public_send attribute != nil
%strong= attribute.to_s.capitalize
= @person.public_send attribute
#person/show.haml
%p= render 'attribute' :attribute => :name
%p= render 'attribute' :attribute => :gender
%p= render 'attribute' :attribute => :age
%p= render 'attribute' :attribute => :address