不明白这个Rails错误消息

时间:2011-09-27 06:31:30

标签: ruby-on-rails

我正在使用从一本名为Agile Web Development with Rails第4版的书中下载的代码。它为Rails 3.05和Rails 3.1提供代码。我正在使用后者...当我尝试在服务器中加载它时,我不理解它产生的错误消息。

如果你能为新手解释,我将不胜感激。

这是为产品

创建的索引
<h1>Listing products</h1>

<table>
<% @products.each do |product| %>
  <tr class="<%= cycle('list_line_odd', 'list_line_even') %>">

    <td>
      <%= image_tag(product.image_url, :class => 'list_image') %>
    </td>

    <td class="list_description">
      <dl>
        <dt><%= product.title %></dt>
        <dd><%= truncate(strip_tags(product.description),
               length: 80) %></dd>
      </dl>
    </td>

    <td class="list_actions">
      <%= link_to 'Show', product %><br/>
      <%= link_to 'Edit', edit_product_path(product) %><br/>
      <%= link_to 'Destroy', product, 
                  confirm: 'Are you sure?',
                  method: :delete %>
    </td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New product', new_product_path %>

这是错误消息

SyntaxError in Products#index

Showing /Users/michaeljohnmitchell/Sites/peep/app/views/products/index.html.erb where line #15 raised:

compile error
/Users/michaeljohnmitchell/Sites/peep/app/views/products/index.html.erb:15: 
syntax error, unexpected ':', expecting ')'
               length: 80) );@output_buffer.safe_concat('</dd>
                      ^
/Users/michaeljohnmitchell/Sites/peep/app/views/products/index.html.erb:23: 
syntax error, unexpected ':', expecting ')'
                  confirm: 'Are you sure?',
                          ^
/Users/michaeljohnmitchell/Sites/peep/app/views/products/index.html.erb:23: 
syntax error, unexpected ',', expecting ')'
Extracted source (around line #15):

12:       <dl>
13:         <dt><%= product.title %></dt>
14:         <dd><%= truncate(strip_tags(product.description),
15:                length: 80) %></dd>
16:       </dl>
17:     </td>
18: 
Trace of template inclusion: app/views/products/index.html.erb

Rails.root: /Users/michaeljohnmitchell/Sites/peep

Application Trace | Framework Trace | Full Trace
app/views/products/index.html.erb:36:in `compile'
app/controllers/products_controller.rb:7:in `index'

2 个答案:

答案 0 :(得分:3)

尝试使用ruby 1.8样式的哈希文字:

<%= image_tag(product.image_url, :class => 'list_image') %>

更新:由于您使用的是ruby 1.8.7,而不是1.9,所有,您的哈希值必须采用以下格式:

<%= image_tag(product.image_url, :class => 'list_image') %>
<%= truncate(strip_tags(product.description), :length => 80) %>
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %>

此外,还有其他任何事情都会影响你。

答案 1 :(得分:1)

选项必须是image_tag @ http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag

的哈希值
<%= image_tag(product.image_url, :class => 'list_image') %>