将Haml转换为erb错误

时间:2013-08-13 18:44:19

标签: ruby-on-rails haml erb

您好我正在尝试将haml转换为erb而不确定我哪里出错了,以下是我的HAML代码

%h1 All Movies

  %table#movies
    %thead
      %tr
        %th{:class => ('hilite' if @sort == 'title') }
= link_to 'Movie Title', movies_path(:sort_param => 'title'), :id => 'title_header'
        %th{:class => ('hilite' if @sort == 'release_date') }
= link_to 'Release Date', movies_path(:sort_param => 'release_date'), :id=> 'release_date_header'
  %th Release Date
  %th More Info
  %th Edit Info
%tbody
- @movies.each do |movie|
  %tr
    %td= movie.title
    %td= movie.rating
    %td= movie.release_date
    %td= link_to "More about #{movie.title}", movie_path(movie) 
    %td= link_to "Edit Movie", edit_movie_path(movie)

= link_to 'ADD NEW MOVIE', new_movie_path

以下是我的错误代码

<h1> All Movies </h1>

<table id='movies'>
<thead>
<tr>
  <th class ='hilite'><%= if @sort == 'title'
     link_to movies_path(:sort_param => 'title'), :id => 'title_header'
end %></th>
  <th class = 'hilite'><%=if @sort == 'release_date'
     link_to movies_path(:sort_param => 'release_date'), :id=> 'release_date_header' end %> </th>
  <th> Release Date </th>
  <th> More Info </th>
  <th> Edit Info </th>
</tr>
</thead>
<tbody>
  <%= @movies.each do |movie| %>
      <tr>
         <td><%= movie.title %> </td>
         <td><%= movie.rating %> </td>
         <td><%= movie.release_date %> </td>
         <td><%= link_to "More about #{movie.title}", movie_path(movie) %> 
         <td><%= link_to "Edit Movie", edit_movie_path(movie) %></td>
  </tr>
   <%= end %>
 <%= link_to 'ADD NEW MOVIE', new_movie_path %>

 </tbody>   
 </table>

这是我得到的错误,

appname/app/views/movies/index.html.erb:25: syntax error, unexpected keyword_end
     ');@output_buffer.append= ( end );@output_buffer.safe_concat('
                                    ^
appname/app/views/movies/index.html.erb:30: syntax error, unexpected keyword_ensure, expecting ')'
appname/app/views/movies/index.html.erb:32: syntax error, unexpected keyword_end, expecting ')'

1 个答案:

答案 0 :(得分:3)

问题似乎是您在<%=电话和each上使用end。从两者中删除等号=,如下所示:

<% @movies.each do |movie| %> 
  <tr>
     <td><%= movie.title %> </td>
     <td><%= movie.rating %> </td>
     <td><%= movie.release_date %> </td>
     <td><%= link_to "More about #{movie.title}", movie_path(movie) %> 
     <td><%= link_to "Edit Movie", edit_movie_path(movie) %></td>
   </tr>
<% end %> 

请注意<%= %>用于打印它们之间的内容。

添加:

另外,当您使用一个if语句时,就像使用了<%= if ...一样,您需要使用if...then...end语法,如下所示:

<th class ='hilite'><%= if @sort == 'title' then link_to movies_path(:sort_param => 'title'), :id => 'title_header' end %></th>
<th class = 'hilite'><%= if @sort == 'release_date' then link_to movies_path(:sort_param => 'release_date'), :id=> 'release_date_header' end %></th>