RoR | 如何嵌套content_tags?

5

如您所见,我有一个带有方法的辅助程序,我正在尝试将其呈现到视图中。

嵌套的content_tags没有呈现出这个标签的含义是什么?

def draw_calendar(selected_month, month, current_date)
  content_tag(:table) do

    content_tag(:thead) do

      content_tag(:tr) do

        I18n.t(:"date.abbr_day_names").map{ |day| content_tag(:th, day, :escape => false) }

      end #content_tag :tr
    end #content_tag :thead 

    content_tag(:tbody) do 

      month.collect do |week| 

        content_tag(:tr, :class => "week") do

          week.collect do |date| 

            content_tag(:td, :class => "day") do

              content_tag(:div, date.day, :class => (Date.today == current_date ? "today" : nil))

            end #content_tag :td
          end #week.collect
        end #content_tag :tr
      end #month.collect
    end #content_tag :tbody 
  end #content_tag :table
end #draw_calendar

以下是有效的解决方案。再次感谢mu太短!

def draw_calendar(selected_month, month, current_date)
  tags = []

  content_tag(:table) do

    tags << content_tag(:thead, content_tag(:tr, I18n.t("date.abbr_day_names").collect { |name| content_tag :th, name}.join.html_safe))

    tags << content_tag(:tbody) do 

      month.collect do |week| 

        content_tag(:tr, :class => "week") do

          week.collect do |date| 

            content_tag(:td, :class => "day") do

              content_tag(:div, :class => (Date.today == current_date ? "today" : nil)) do

                date.day.to_s

              end #content_tag :div
            end #content_tag :td
          end.join.html_safe #week.collect
        end #content_tag :tr
      end.join.html_safe #month.collect
    end #content_tag :tbody 
    tags.join.html_safe
  end #content_tag :table
end #draw_calendar

结束


类似于这个:https://dev59.com/mW855IYBdhLWcg3wrGZY 你错过了一个 +(或其他任何字符),用来连接 theadtbody - PeterWong
1个回答

10
你遇到的问题是 content_tag 要求它的块返回一个字符串,你可以通过跟踪代码来看它使用了 CaptureHelper 中的 capture,并忽略块中任何非字符串的返回值。
你需要使用以下代码将你的collect转换成字符串:
content_tag(:tbody) do 
  month.collect do |week| 
    content_tag(:tr, :class => "week") do
      week.collect do |date|
        ..
      end.join.html_safe
    end
  end.join.html_safe
end
例如,像这样的一个辅助函数:
content_tag(:table) do
  content_tag(:thead) do
    content_tag(:tr) do
      [1,2,3,4].map do |i|
        content_tag(:td) do
          "pancakes #{i}"
        end
      end
    end
  end
end

输出:

<table>
    <thead>
        <tr></tr>
    </thead>
</table>

但是添加.join.html_safe

content_tag(:table) do
  content_tag(:thead) do
    content_tag(:tr) do
      [1,2,3,4].map do |i|
        content_tag(:td) do
          "pancakes #{i}"
        end
      end.join.html_safe
    end
  end
end

可以生成预期结果:

<table>
    <thead>
        <tr>
            <td>pancakes 1</td>
            <td>pancakes 2</td>
            <td>pancakes 3</td>
            <td>pancakes 4</td>
        </tr>
    </thead>
</table>

非常感谢,这确实帮助我更好地理解了我一直在努力弄清楚的所有内容。 :) 2+ 有用的。 - Polygon Pusher
1
@DigitalCake:不幸的是,如果你想在Rails中做一些非常规的事情,你最终会变得熟悉Rails源代码或者需要进行大量的猜测和实验。至少这是我的经验。 - mu is too short

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接