我正在尝试编写一个视图助手,但它没有返回正确的东西。
def table_layout_for(object, *attrs)
content_tag :table, :class => "layout" do
for a in attrs
content_tag :th do
object.class.human_attribute_name(a)
end
content_tag :td do
object.send(a) if object.respond_to?(a)
end
end
end
end
<%= table_layout_for(@user, :email, :full_name, :business_name, :phone_number) %>
[:email, :full_name, :business_name, :phone_number]
<table class="layout">
<tr>
<th>Email</th>
<td>paulmacek@gmail.com</td>
</tr>
<tr>
<th>Full name</th>
<td>Paul Macek</td>
</tr>
<tr>
<th>Business name</th>
<td>2bynight</td>
</tr>
<tr>
<th>Phone number</th>
<td>555-423-1245</td>
</tr>
</table>
答案 0 :(得分:1)
这是因为您的content_tag中的块返回的内容是您的数组,而不是所有content_tag。
你需要做出:
def table_layout_for(object, *attrs)
content_tag :table, :class => "layout" do
ret = ''
for a in attrs
ret += content_tag :th do
object.class.human_attribute_name(a)
end
ret += content_tag :td do
object.send(a) if object.respond_to?(a)
end
end
ret
end
end