使用 Rails 查看助手返回错误值 [Rails、Ruby]
我正在尝试编写一个视图助手,但它没有返回正确的内容。
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>[email protected]</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>
I am trying to write a view helper but it's not returning the correct thing.
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
This problem:
<%= table_layout_for(@user, :email, :full_name, :business_name, :phone_number) %>
Returns this:
[:email, :full_name, :business_name, :phone_number]
I'd like it to return this:
<table class="layout">
<tr>
<th>Email</th>
<td>[email protected]</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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的 content_tag 中的块返回的内容是您的数组,而不是所有 content_tag。
你需要做:
it's because the content return by your block in your content_tag is your array, not all content_tag.
You need made :