如何在rails中创建html辅助表函数?
我想在ruby中创建helper方法以生成如下表:如何在rails中创建html辅助表函数?,html,ruby-on-rails,ruby,Html,Ruby On Rails,Ruby,我想在ruby中创建helper方法以生成如下表: <table> <legend>Test Table</legend> <thead> <th>name</th> <th>age</th> <th>occupation</th> </thead> <tbody> <tr><td>Josh</td><td
<table>
<legend>Test Table</legend>
<thead>
<th>name</th>
<th>age</th>
<th>occupation</th>
</thead>
<tbody>
<tr><td>Josh</td><td>32</td><td>Ditch Digger</td></tr>
<tr><td>John</td><td>54</td><td>Burger Flipper</td></tr>
<tr><td>Jake</td><td>21</td><td>Couch Potato</td></tr>
</tbody>
</table>
root
users
{'name'=>'Josh', 'age'=>'32','job'=>'Ditch Digger'},
{'name'=>'John', 'age'=>'54','job'=>'Burger Flipper'},
{'name'=>'Jake', 'age'=>'21','job'=>'Couch Potato'}
尝试使用函数生成此表
def data_table_personnel(source)
if subdata = source&.dig('root', 'users')
h.content_tag :table do
h.content_tag :tr do
h.content_tag :td subdata.name
h.content_tag :td subdata.age
h.content_tag :td subdata.job
end
end
end
end
end
我在我的项目中制作了很多表格,所以如果可能的话,我想避免ERB def数据表人员(来源)
def data_table_personnel(source)
users = source&.dig('root', 'users')
return if users.blank?
h.content_tag :table do
users.map do |user|
# generate '<tr>' for each user
h.content_tag :tr do
[:name, :age, :job].map do |attr|
# generate '<td>' for each user attribute
h.content_tag :td user[attr]
end.join # join <td>s
end
end.join # join <tr>s
end
end
users=源和.dig('root','users'))
如果users.blank返回?
h、 内容标签:表do
users.map do | user|
#为每个用户生成“”
h、 内容标签:tr do
[:姓名,:年龄,:职务].map do | attr|
#为每个用户属性生成“”
h、 内容标签:td用户[attr]
end.join#join s
结束
end.join#join s
结束
结束