Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR | how to get content_tags to nest?

As you can see I have a helper with a method that I'm trying to render out to the view.

The nested content_tags do not render what is my disconnect about this tag?

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

::: EDIT :::

So here is what worked. Thanks again mu is too short!

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

end

like image 604
Polygon Pusher Avatar asked Jun 01 '12 04:06

Polygon Pusher


1 Answers

Your problem is that content_tag wants its block to return a string, you can trace through the code to see that it uses capture from CaptureHelper and that ignores any non-string return from the block.

You need to turn your collects into strings with something like this:

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

For example, a helper like this:

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

produces:

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

but adding the .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

produces the expected:

<table>
    <thead>
        <tr>
            <td>pancakes 1</td>
            <td>pancakes 2</td>
            <td>pancakes 3</td>
            <td>pancakes 4</td>
        </tr>
    </thead>
</table>
like image 148
mu is too short Avatar answered Nov 03 '22 20:11

mu is too short