我正在创建一个 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。请帮我。谢谢