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 [email protected]? -%> </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
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