magmike
magmike

Reputation: 473

Convert slim to ERB

I am making my first steps in ruby and I am trying to learn how to use Sinatra. In a relevant book I found this example view written in Slim.

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!

I tried to change it into ERB ending up in something like this

<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>

Sinatra gave me this report

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 ')'

Can you give me a clue of what is going on? Thank you in advance.

Upvotes: 0

Views: 1082

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

Put another <% end %> right after your <ul>... line (before the <% else %>).

In Slim, the end of the @songs.each loop is implied by the indentation, but in ERB you need to explicitly end it.

Also, you will need to use ERB tags in your <a> element too. And you'll probably want the <ul> outside of the list. Here's what the the whole thing should probably look like:

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

Upvotes: 2

Related Questions