使用content_标记在rubyonrails中构建HTML表
我正试图借助content_标记方法在RubyonRails中构建一个html表 例如:使用content_标记在rubyonrails中构建HTML表,html,ruby-on-rails,ruby,html-table,content-tag,Html,Ruby On Rails,Ruby,Html Table,Content Tag,我正试图借助content_标记方法在RubyonRails中构建一个html表 例如: @template.content_tag(:tr, @template.content_tag(:td, options[:argument0])) 这应该会导致 <tr><td>content of argument0</td></tr> argument0的内容 我需要的是 <tr><td>content of argume
@template.content_tag(:tr, @template.content_tag(:td, options[:argument0]))
这应该会导致
<tr><td>content of argument0</td></tr>
argument0的内容
我需要的是
<tr><td>content of argument0</td><td>argument1</td> ... </tr>
argument0argument1的内容。。。
我可以用同样的方法构建它吗?如何传递两个内容标签 您可以使用
concat
:
@template.content_tag(:tr,
@template.content_tag(:td, options[:argument0]).
concat(@template.content_tag(:td, options[:argument1])).
concat(@template.content_tag(:td, options[:argument2]))
# ....
)
或者按照评论中的建议使用循环
rows = returning "" do |cells|
5.times do |i|
cells.concat @template.content_tag(:td, options["argument#{i}".to_sym]
end
end
@template.content_tag(:tr,rows)
您可以使用
concat
:
@template.content_tag(:tr,
@template.content_tag(:td, options[:argument0]).
concat(@template.content_tag(:td, options[:argument1])).
concat(@template.content_tag(:td, options[:argument2]))
# ....
)
或者按照评论中的建议使用循环
rows = returning "" do |cells|
5.times do |i|
cells.concat @template.content_tag(:td, options["argument#{i}".to_sym]
end
end
@template.content_tag(:tr,rows)