【问题标题】:Converting Haml to erb error将 Haml 转换为 erb 错误
【发布时间】:2013-08-13 18:44:18
【问题描述】:

您好,我正在尝试将 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

下面是我的erb代码

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

【问题讨论】:

    标签: ruby-on-rails haml erb


    【解决方案1】:

    问题似乎是您在each 呼叫和end 上使用&lt;%=。从两者中删除等号=,如下所示:

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

    请注意,&lt;%= %&gt; 用于打印它们之间的内容。

    补充:

    此外,当您使用一个衬垫 if 语句时,就像您使用的那样,即 &lt;%= 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>
    

    【讨论】:

    • 我无法在 erb 中添加标题“电影标题”名称,= link_to '电影标题', movies_path(:sort_param => 'title'), :id => 'title_header' %th {:class=> ('hilite' if @sort == 'release_date') } 你能帮我把它添加到 erb 中吗
    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多