加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RoR |如何让content_tags嵌套?

发布时间:2020-12-17 03:46:26 所属栏目:百科 来源:网络整理
导读:正如你所看到的,我有一个帮助器,我正在尝试渲染到视图中. 嵌套的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.abb
正如你所看到的,我有一个帮助器,我正在尝试渲染到视图中.

嵌套的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,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 => (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

结束

解决方法

你的问题是 content_tag希望它的块返回一个字符串,你可以通过代码来查看它是否使用了来自CaptureHelper的 capture并且忽略了来自块的任何非字符串返回.

你需要把你的收藏变成字符串,如下所示:

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,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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读