Ruby on rails 在Rails的一个方法中使用多个content_标记
我正在尝试将Rails 2.3应用程序更新为更新的Rails版本(4/5)。Ruby on rails 在Rails的一个方法中使用多个content_标记,ruby-on-rails,ruby-on-rails-4,html-table,Ruby On Rails,Ruby On Rails 4,Html Table,我正在尝试将Rails 2.3应用程序更新为更新的Rails版本(4/5)。 我有一个方法,使用列表作为输入打印html表,调用方可以自定义行的显示。我还搜索了一些做类似事情的现有gem,但它们没有我需要的所有要求。所以我必须让这一切顺利。代码是 def model_table_2(collection, headers, options = {}, &proc) options.reverse_merge!({ :id => nil
我有一个方法,使用列表作为输入打印html表,调用方可以自定义行的显示。我还搜索了一些做类似事情的现有gem,但它们没有我需要的所有要求。所以我必须让这一切顺利。代码是
def model_table_2(collection, headers, options = {}, &proc)
options.reverse_merge!({
:id => nil,
:class => nil,
:style => nil,
:placeholder => 'Empty',
:caption => nil,
:summary => nil,
:footer => nil
})
placeholder_unless !collection.empty?, options[:placeholder] do
html_opt = options.slice(:id, :class, :style, :summary)
content_tag(:table, html_opt) do
table_sections = []
table_sections << content_tag(:caption, options[:caption]).to_s if options[:caption]
table_sections << content_tag(:thead,
content_tag(:tr,
headers.collect { |h|
concat(content_tag(:th, h))
}
)
)
if options[:footer]
table_sections << content_tag(:tfoot,
content_tag(:tr, content_tag(:th, concat(options[:footer]), :colspan => headers.size))
)
end
table_sections << content_tag(:tbody,
collection.each_with_index.collect do |row, row_index|
concat(
proc.call(row, cycle('odd', 'even'), row_index)
)
end.join
)
table_sections.join
end
end
end
def placeholder(message = t('general.empty'), options = {}, &proc)
# set default options
o = { :class => 'placeholder', :tag => 'p' }.merge(options)
# wrap the results of the supplied block, or just print out the message
if proc
t = o.delete(:tag)
concat tag(t, o, true), proc.binding
yield
concat "</#{t}>", proc.binding
else
content_tag o.delete(:tag), message, o
end
end
def placeholder_unless(condition, *args, &proc)
condition ? proc.call : concat(placeholder(args), proc.binding)
end
def model_table_2(集合、头、选项={},&proc)
选项。反向合并!({
:id=>nil,
:class=>nil,
:style=>nil,
:占位符=>“空”,
:caption=>nil,
:summary=>nil,
:footer=>nil
})
占位符_除非!collection.empty?,选项[:占位符]do
html_opt=options.slice(:id,:class,:style,:summary)
内容标签(:table,html\u opt)do
表_节=[]
表|部分‘模型表测试’,:footer=>The footer})do |记录,klass,行|索引|-%>
klass+(记录[:活动]?“”:“文本静音”)do-%>
klass do-%>
*
渲染形式
但结果并不是我所期望的:
<table class="table table-bordered">
<th>No.</th>
<th>Name</th>
The Footer
<tr class="even">
<td>1</td>
<td>First</td>
</tr>
<tr class="odd">
<td>2</td>
<td>Second</td>
</tr>
<tr class="even text-muted">
<td>3</td>
<td>Last</td>
</tr>
<tr class="odd">
<td>*</td>
<td>render form</td>
</tr>
</table>
不
名称
页脚
1.
弗斯特
2.
第二
3.
最后
*
渲染形式
如您所见,缺少一些标记,如标题
,标题
,正文
,tfoot
。我猜这是因为content\u标记
调用是嵌套的。我以前尝试过不使用table_部分
数组,但也不起作用
另外,当列表为空时,我有一个错误,代码转到
占位符…
方法。这是一个奇怪的内容标签,但如果你嵌套它们,你需要。否则,您将得到最后一个返回的字符串,而内部标记将消失在以太中。不幸的是,根据我的经验,我发现复杂的嵌套不值得转移到辅助方法
也许,更好的方法是使用decorator模式、rails partials或使用类似的东西来干涸html。这是一种奇怪的内容标签,但是如果你嵌套它们,你需要这样做。否则,您将得到最后一个返回的字符串,而内部标记将消失在以太中。不幸的是,根据我的经验,我发现复杂的嵌套不值得转移到辅助方法
也许,更好的方法是使用装饰器模式、rails片段或使用类似的东西来去除html。嵌套
content\u标记
应该可以。看看你的代码,我认为有多个partial是一个更干净、更简单的解决方案。嵌套content\u tag
应该就行了。看看你的代码,我认为有多个partials是一个更干净、更简单的解决方案。你的回答真的帮助了我。我使用一个concat
为我放入table\u部分的每个元素:table\u部分使用一个concat
使其工作,这是因为concat
现在有一个参数,过去有2个。你的回答真的帮助了我。我使用一个concat
为我放入table_部分的每个元素创建了一个concat
:table_部分,这是因为concat
现在有一个参数,而在过去它有两个参数。
<table class="table table-bordered">
<th>No.</th>
<th>Name</th>
The Footer
<tr class="even">
<td>1</td>
<td>First</td>
</tr>
<tr class="odd">
<td>2</td>
<td>Second</td>
</tr>
<tr class="even text-muted">
<td>3</td>
<td>Last</td>
</tr>
<tr class="odd">
<td>*</td>
<td>render form</td>
</tr>
</table>