Search code examples
ruby-on-railsrubyhelper

How to show organized data from rails helper content tag


How to show organized data from rails helper content tag?

Like below is my helper method and I want to show all categories name grouped by a parent, and that is ul li if you can please see below method I think you will understand that code & what do I want. That method outputted the data but not with ul li

The helper method

def category
    parent_categories = Category.select(:id, :name, :parent).group_by(&:parent)
    parent_categories.each do |parent, childs|
        content_tag(:div) do 
            content_tag(:h1, parent)
        end +
        content_tag(:ul) do 
            childs.each do |child|
                content_tag(:li, child.name)
            end
        end
    end
end

the output of <%= category %>

{"Technology"=>[#<Category id: 1, name: "Programming", parent: "Technology">, #<Category id: 3, name: "Ruby on Rails", parent: "Technology">, #<Category id: 9, name: "Full Time", parent: "Technology">, #<Category id: 14, name: "Business Opportunities", parent: "Technology">, #<Category id: 15, name: "Contract & Freelance", parent: "Technology">, #<Category id: 18, name: "Engineering", parent: "Technology">, #<Category id: 25, name: "IT", parent: "Technology">], 

"Education"=>[#<Category id: 5, name: "Industry", parent: "Education">, #<Category id: 6, name: "Education", parent: "Education">, #<Category id: 7, name: "Education & Industry", parent: "Education">, #<Category id: 16, name: "Customer Service", parent: "Education">, #<Category id: 17, name: "Diversity Opportunities", parent: "Education">],

"Other"=>[#<Category id: 8, name: "Part Time", parent: "Other">, #<Category id: 12, name: "Admin & Clerical", parent: "Other">]}

the schema.rb

create_table "categories", force: :cascade do |t|
  t.string "name"
  t.string "parent"
end

That was my done works.

After the example is what do I want like

Technology (Parent)

  • Programming
  • Ruby on Rails
  • Ruby
  • ReactJS

Education (Parent)

  • Office
  • Teacher
  • Physics

Other (Parent)

  • Clinical
  • Helth
  • etc...

Please help out me to done this works.

Thanks


Solution

  • You are using ERB in your helper but it is not an html.erb file so you don't get the tags you are trying to create. Why not just use the hash you've produced and then I think what you are looking for is something like:

    The helper method:

    def category
      Category.select(:id, :name, :parent).group_by(&:parent)
    end
    

    And in your view file (.html.erb) do something like:

      <% category.each do |cat, list| %>
        <div class="category">
          <b> <%= cat %> </b>
          <ul>
            <% list.each do |item| %>
              <li> <%= item.name %> </li>
            <% end %>
          </ul>
        </div>
        <br>
      <% end %>
    

    OK, you can do it the way you are proposing using the concat method according to the documentation:

    def category
        parent_categories = Category.select(:id, :name, :parent).group_by(&:parent)
        parent_categories.each do |parent, childs|
            concat content_tag(:div) do 
               concat content_tag(:h1, parent)
            end 
           concat content_tag(:ul) do 
                childs.each do |child|
                   concat content_tag(:li, child.name)
                end
            end
        end
    end