我正在创建一个rails应用程序,我必须在其中创建一个教程网站,因为我想在我的索引页面上显示课程列表,并且用户将选择一个课程,它将重定向到其不同的显示页面那些课程的部分和小节都在那里,在一个部分下可以有任何小节。 **For example**
Course is Ruby on rails,
在此之下,我们有像
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.
我想在我的应用程序中使用这种嵌套的li。
我已经在我的帮助窗体中编写了嵌套li的代码。我的courses_helper.rb
文件中的代码是
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
在我的show page
我称之为:
<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 -->
任何人都可以帮我解决这个问题。正如我在上面的示例中所示,此代码无法正确嵌套我的子部分。我已经使用了closure_tree gem。请帮我。谢谢
答案 0 :(得分:0)
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