2013-06-24 40 views
0

我正在我的紅寶石的第一步,我想學習如何使用Sinatra。在一本相關的書中,我發現了用Slim編寫的這個示例視圖。轉換苗條ERB

h1 Songs 
a href="/songs/new" Create a new song 
- if @songs.any? 
    ul#songs 
    [email protected] do |song| 
     li <a href="/songs/#{song.id}">#{song.title}</a> 
- else 
    p No songs have been created yet! 

我試圖把它變成ERB在這樣的

<html> 
<h1> Songs </h1> 
<a href="/songs/new" Create a new song></a> 
<% if @songs.any? %> 
    <%#songs%> 
    <% @songs.each do |song|%> 
     <ul><li> <a href="/songs/#{song.id}"><%=#{song.title}%></a></li></ul> 
<% else %> 
    <p> No songs have been created yet!</p> 
<% end %> 
</html> 

西納特拉結束了給了我這個報告

SyntaxError at /songs 
Documents/programs/sinatra/views/songs.erb:8: syntax error, unexpected keyword_else, expecting ')' ; else^
Documents/programs/sinatra/views/songs.erb:10: syntax error, unexpected keyword_end, expecting ')' ; end ; @_out_buf.concat "\t\n"^
Documents/programs/sinatra/views/songs.erb:14: syntax error, unexpected keyword_ensure, expecting ')' 
Documents/programs/sinatra/views/songs.erb:16: syntax error, unexpected keyword_end, expecting ')' 

你能給我是怎麼回事的線索? 預先感謝您。

回答

2

把另一個<% end %>放在你的<ul>...之後(在<% else %>之前)。

在Slim中,01​​循環的末尾是由縮進隱含的,但在ERB中您需要明確地end它。

此外,您還需要在您的<a>元素中使用ERB標籤。而且你可能需要<ul>以外的列表。以下是整件事應該看起來像什麼:

<ul> 
    <% @songs.each do |song| %> 
    <li><a href="/songs/<%= song.id %>"><%= song.title %></a></li> 
    <% end %> 
</ul> 
+0

非常感謝你Dylan.You一直很有幫助! –