1

我在 Heroku 托管的 Ruby on Rails 3.2.13 应用程序中遇到了一个奇怪的错误,我无法在本地复制,也无法确定原因。

以下是症状:

在几页上,我看到可怕的“我们很抱歉,但出了点问题”错误:

在此处输入图像描述

日志表明存在模板错误:

  Started GET "/users/sign_in" for 107.203.222.41 at 2013-05-27 13:45:18 +0000

  ActionView::Template::Error (syntax error on line 6, col 15: `'):
   15:   <%= f.password_field :password %></div>
   16: 
   21:   <div><%= f.submit "Sign in" %></div>
   19:   <% end -%>
   17:   <% if devise_mapping.rememberable? -%>
   18:     <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
 app/views/devise/sessions/new.html.erb:8:in `_app_views_devise_sessions_new_html_erb__2053461519121638579_44147620'
 app/views/devise/sessions/new.html.erb:18:in `block in _app_views_devise_sessions_new_html_erb__2053461519121638579_44147620'
   20: 

注意行号是如何乱序的。

这是另一个页面的示例:

  Started GET "/blog" for 107.203.222.41 at 2013-05-27 13:36:10 +0000

  ActionView::Template::Error (syntax error on line 6, col 15: `'):
  10:     <%= render :partial => "post", :collection => @posts %>
  13:   <% end %>

  9:   <%= content_tag(:div, :id => 'blog_content') do %>
  11: 
  12:     <%= will_paginate @posts, :page_links => false %>
  14: 
  15:   <%= render 'sidebar' %>
  app/views/posts/blog.html.erb:12:in `block (2 levels) in _app_views_posts_blog_html_erb___3437273290190956225_40741600'
  app/views/posts/blog.html.erb:9:in `block in _app_views_posts_blog_html_erb___3437273290190956225_40741600'
  app/views/posts/blog.html.erb:7:in `_app_views_posts_blog_html_erb___3437273290190956225_40741600'

知道什么会导致这个错误吗?如何准确识别“第 6 行,第 15 列”指的是什么?

非常感激!

编辑

这对我来说毫无意义,但这是导致问题的行:

 <% if devise_mapping.rememberable? -%>
   <div>
        <%= f.check_box :remember_me %>
        <%#= f.label :remember_me %>
    </div>
 <% end -%>

如果我注释掉 f.label 行,页面加载正常。如果我取消注释,我会收到语法错误。什么会导致这个?

编辑 2

我从头开始重新创建文件并输入代码的相关部分:

<%= form_for(resource, :as => resource_name, :url => session_url(resource_name), :autocomplete => 'off') do |f| %>
    <div>
         <%= f.check_box :remember_me %>
         <%#= f.label :remember_me %>
    </div>
<% end %>

同样的问题:当 f.label 行被评论时,页面加载。取消注释时,我得到相同的语法错误:

 ActionView::Template::Error (syntax error on line 6, col 15: `')
4

1 回答 1

1

原来这是 en.yml 中的 YAML 的问题。

具体来说,YAML 最初是:

# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
  activerecord:
    models:
    attributes:

如果将其插入http://yamllint.com/之类的内容,则会收到我在生产日志中看到的错误消息:

syntax error on line 6, col 15: `'

将此更改回 Rails 默认值可解决问题:

# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
  hello: "Hello world"

这个线程有更多关于在 Rails 中解析 YAML 的信息,这让我找到了原因:rails error, could't parse YAML

于 2013-05-27T16:53:44.657 回答