Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:58:32+00:00 2026-06-09T12:58:32+00:00

I am creating a rails application, in which i have to create a tutorials

  • 0

I am creating a rails application, in which i have to create a tutorials website, for that i want to display list of courses on my index page and as the user will select a course it will redirect to its show page where different sections and subsection for that courses are there,Under one section there can be any no of subsections. **For example**
Course is Ruby on rails, under this we have section like

1.Introduction
 1.1 What is ruby
 1.2 Small program in ruby
  1.2.1 Say hello world
   1.2.1.1 Welcome user
   1.2.1.2 array in ruby
  1.2.2 User Interface
 1.3 Introduction to irb
2. Introduction to rails.

I want this kind of nested li in my application.

I have written code for nested li in my helper form.Code in my courses_helper.rb file is

def nested_li(objects)
  output = ""

  output += "<ul>"
  path = [nil]
  objects.each do |o|
    if o.parent_id != path.last
      # we are on a new level, did we decent or ascent?
      if path.include?(o.parent_id)
        # remove wrong wrong tailing paths elements
        while path.last != o.parent_id
          path.pop
          output += "\n</ul>"
        end
      else
        path << o.parent_id
        output += "\n<ul>"
      end
    end
    output += "\n<li>#{yield o}</li>"
  end
  #pop off unfinished paths elements
  while path.length > 1
    path.pop
    output += "\n</ul>"
  end
  output += "\n</ul>"
  return output.html_safe
end

And in my show page I an calling it like this:

<div  class="row">
  <div id="main_content" class="span9">
  <h2><%= @course.title.titleize if !@course.title.nil? -%> </h2>
  <p> <%= @course.notes %></p>  

  <h3> List </h3>

  <div class="section">  


            <ul>   
                <%= nested_li(@sections) do |section| %>  
              <%= link_to section.title,section_path(section.id) %>
              <% end %>  
             </ul>




 </div>


  </div><!-- /main_content -->
   <%= render :partial => "/shared/side_bar" %>
</div><!-- /row -->

Can any one help me how to solve this issue. This code is not able to call my subsection nested properly as I have shown in example above. I have use closure_tree gem for it. Please help me. Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:58:33+00:00Added an answer on June 9, 2026 at 12:58 pm
    def display_sections(sections, course_id)
    section_array = []
           <ul class='nav nav-list '>"
      for section in sections
       if(!section_array.include?(section.id))
           if section.children.present?
              ret += "<li class='item'>"
            ret += "<div id='sectiontitle_#{section.id}' class='section_title'>" 
            ret +="<span id='section_#{section.id}'>"            
            ret += "</span>"
            ret += "<br/>"
            ret += "</div>"
              ret += find_all_subsections(section, section_array, course_id)
    
              ret += "</li>"
    
              section_array << section.id
          else
             ret += "<li class='item'>"
             ret += "<div id='sectiontitle_#{section.id}' class='section_title'>"
               ret += link_to section.title,course_section_path(course_id, section.id), :title=> "#{section.id}"
             ret +="<span id='section_#{section.id}'>"
             ret += "</span>"
             ret += "</div>"
               ret += "</li>"
    
    
               section_array << section.id
           end
       end
      end
      ret += "</ul>"
      ret.html_safe
      end
    
     def find_all_subsections(section, section_array, course_id)
    
      if section.children.size > 0
        ret = '<ul class="nav nav-list">'
        section.children.each { |subsection|
          section_array << subsection.id
          if subsection.children.present?
            ret += "<li>"
            ret += "<div id='sectiontitle_#{subsection.id}' class='section_title'>"
            ret += link_to h(subsection.title),course_section_path(course_id, subsection.id), :class => 'sub_item', :title=> "#{subsection.id}"
            ret += "<br/>"
            ret +="<span id='section_#{subsection.id}'>"            
            ret += "</span>"
            ret += "</div>"
            ret += find_all_subsections(subsection, section_array, course_id)
            ret += '</li>'
    
          else
            ret += "<li class='item'>"
            ret += "<div id='sectiontitle_#{subsection.id}' class='section_title'>"
            ret += link_to h(subsection.title),course_section_path(course_id, subsection.id), :class => 'sub_item', :title=> "#{subsection.id}"
            ret +="<span id='section_#{subsection.id}' style='display:none;' class='pull-right'>"
            ret += "<br/>"
    
            ret += "</div>"
            ret += '</li>'
            section_array << subsection.id
          end
          }
        ret += '</ul>'
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a Rails application which uses MySQL. I have a table in my
I am creating a rails application that needs to store a large amount of
I am building a Rails application which allows a user to create an object
I'm creating a new web application (Rails 3 beta), of which pieces of it
I am working on a Rails application that has user authentication which provides an
I have a rails application that displays content that is being rendered as a
In my Rails application, I have a variety of database tables that contain user
So I have a Rails application that upon user submission should generate some kind
I have a Rails application with several models-views-controllers which have some similar characteristics, for
In my Rails application I have an action which creates a XML document using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.