我喜欢使用 Haml 助手,但这些年来情况发生了一些变化。旧方法只是连接到缓冲区。这是我所拥有的:
def confirmation_table(field)
# Be certain that if the user is logged in, his/her email and name show
if field.respond_to? :user
haml_tag('tr') {
haml_tag('th', 'Email:')
haml_tag('td', field.user.email)
}
haml_tag('tr') {
haml_tag('th', 'Name:')
haml_tag('td', field.user.full_name)
}
else
haml_tag('tr') {
haml_tag('th', 'User Information:')
haml_tag('td', 'Not specified.')
}
end
field.class.columns.collect{|col| col.name}.reject{|col|
col =~ /_at$/ ||
col =~ /_on$/ ||
col =~ /_id$/ ||
col == 'id'}.each do |col|
haml_tag('tr') {
haml_tag('th', ActiveSupport::Inflector::humanize(col))
haml_tag('td', typeize(field, col))
}
end
end
当然,在我看来,这可以简单地访问:
- confirmation_table(@f)
但是,(对我而言)返回一个字符串更有意义。我看不出来
haml_capture
提供相同的结构化能力。任何提示?
最佳答案
包裹您的 haml_tag
来电 capture_haml
:
def confirmation_table(field)
capture_haml do
if field.respond_to? :user
haml_tag(:tr) do
haml_tag('th.email', 'Email:')
haml_tag('td.email', field.user.email)
end
# ...
end
end
end
它们将被
capture_haml
捕获并返回.
关于ruby-on-rails - 使用返回字符串的 Haml 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5889076/