无法在Slim的逻辑中迭代数组

时间:2011-11-29 20:34:29

标签: ruby sinatra slim-lang

我正在尝试遍历名为@items的数组。我想通过将它添加到类变量并从SLIM调用它我可能能够输出数组。我做错了什么?

require "rubygems"
require "sinatra"
require "slim"
set :port, 80
get "/" do
  slim :index
end
get "/history" do
  @items = Dir.entries(File.expand_path(File.dirname(__FILE__)) + "/history").to_a
  slim :history
end
__END__
@@layout
doctype html
html
  head
    title Web View
  body
    div id="main" name="main"
      h1 Web View
      == yield
@@index
h2 Available Options
a href="/history" id="history_view" name="history_view" History
@@history
h2 History
- unless items.empty?
  - for item in items do
    a href="#{item}/output.html" item
  - else
    p NO ITEMS FOUND

1 个答案:

答案 0 :(得分:2)

您似乎已尝试在“历史记录”布局中调用items方法。除此之外,请查看else子句的缩进级别。尝试更改您的代码:

h2 History
- unless @items.empty?
  - for item in @items do
    a href="#{item}/output.html" item
- else
    p NO ITEMS FOUND