Rails 中是否可以循环对象的属性?我有一个对象,而不是对视图中的每个属性进行编码,我想在视图中输出它们中的每个属性,因为它们有很多。
我有一个名为@work_profile
它有很多属性,主要是布尔复选框值。
编辑:我知道我可以使用@work_profile.attributes
。任何有关将哈希格式化为更用户友好的内容的帮助都会很棒。
The ActiveRecord::Base.attributes() http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-attributes方法返回所有属性的哈希值。您可以使用它来循环所有属性。
@work_profile.attributes.each do |attr_name, attr_value|
...
end
从某种角度来看,这将给出:
<% @work_profile.attributes.each do |attr_name, attr_value| %>
<div class="work_profile_attribute">
<%= attr_name %>: <%= attr_value %>
</div>
<% end %>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)