1

Hi I am trying to convert haml to erb not sure where i am going wrong, following is my HAML code

%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

And below is my erb code

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

This is the error i am getting,

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

1 回答 1

3

问题似乎是您正在使用<%=on eachcall 和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>
于 2013-08-13T18:49:26.670 回答