ruby-on-railsrubyruby-on-rails-4nestedcontent-tag

nested div tags in rails


I want to achieve this HTML code in my view

<div class="progress ">
  <div class="progress-bar bgclre3559b" role="progressbar" style="width:80%">
    <span><%= top_cat.first %></span>
  </div>
  <div class="progress-bar bgclre3559b" role="progressbar" style="width:15%">
    <span class="clrfff"><%= "#{top_cat.last}%" %></span>
  </div>
</div>

I tried this one but it only shows the second div inside. Is there a way to concatenate them or how to display properly in rails way.

content_tag :div, class: "progress" do
  content_tag :div, class: "progress-bar bgclre3559b", role: "progressbar", style: "width: 80%" do
    content_tag :span, top_cat.first
  end
  content_tag :div, class: "progress-bar bgclre3559b", role: "progressbar", style: "width: 15%" do
    content_tag :span, "#{top_cat.last}%"
  end
end

Thanks in advance!


Solution

  • I found an answer in case if someone need to solve such problem. I changed the code in my helper file like this.

    first = 
        content_tag :div, class: "progress-bar bgclre3559b", role: "progressbar", style: "width: 80%" do
          content_tag :span, top_cat.first
        end
    second =
        content_tag :div, class: "progress-bar bgclre3559b", role: "progressbar", style: "width: 15%" do
          content_tag :span, "#{top_cat.last}%"
        end
    
    content_tag :div, class: "progress" do
      first.concat(second).html_safe
    end