PagesController中的SyntaxError #home unexpected keyword_ensure,期望输入结束

时间:2014-06-13 15:56:04

标签: heroku ruby-on-rails-4 localhost

在localhost上启动rails服务器后。尝试构建一个带有社交登录选项的简单导航栏。当我尝试添加“使用Google登录”和“使用Facebook登录”时,我收到以下错误:

“PagesController #home中的SyntaxError /Users/okazemi/Desktop/testapp/app/views/layouts/_header.html.erb:40:语法错误,意外的keyword_ensure,期待输入结束“

</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
    <% flash.each do |name, msg| %>
        <%= content_tag(:div, msg, class: "alert alert-info") %>

使用“&lt;%= render'layouts / header'%&gt;”以红色突出显示。

这是application.html.rb代码:

<!DOCTYPE html>
<html>
<head>
<title>TestApp</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
    <% flash.each do |name, msg| %>
        <%= content_tag(:div, msg, class: "alert alert-info") %>
    <% end %>
    <%= yield %>
</div>  
</body>
</html>

渲染中引用的_header.html.erb的代码如下:

<nav class="navbar navbar-default" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
 <%= link_to "testapp", root_path, class: "navbar-brand" %>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
  <ul class="nav navbar-nav navbar-right">

    <li><%= link_to "Home", root_path %><li>
    <li><%= link_to "Recruiter", recruiter_path %><li> 
    <li><%= link_to "Employer", employer_path %><li> 
    <li><%= link_to "Postings", postings_path %><li> 
    <li><%= link_to "testing", testing_path %><li> 
    <li><%= link_to "Learn", learn_path %><li> 

    <% if user_signed_in? %> Signed in as <%= current_user.name %>. Not you?
    <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
    <% else %>
    <%= link_to "Sign up", new_user_registration_path %> or
    <%= link_to "Sign in", new_user_session_path %> 
    <%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
    <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
    <% end %>

<% end %>

  </ul>
</div><!-- /.navbar-collapse -->

任何帮助都会有很大帮助。花了好几个小时试图弄清楚我是否在某个地方错过了一个结局,但似乎没有任何结果。谢谢!

1 个答案:

答案 0 :(得分:1)

我在_header.html.erb中看到了一些问题,其中最重要的一个问题是代码中有额外的<% end %>导致语法错误。我在下面的代码中用<!-- Remove this extra end -->条评论标记了它。

<nav class="navbar navbar-default" role="navigation">
<!-- ....... -->

  <% if user_signed_in? %> Signed in as <%= current_user.name %>. Not you?
  <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
  <% else %>
  <%= link_to "Sign up", new_user_registration_path %> or
  <%= link_to "Sign in", new_user_session_path %> 
  <%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
  <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
  <% end %>

<% end %> <!-- Remove this extra end -->
</ul>
</div><!-- /.navbar-collapse -->

一些建议:

<nav class="navbar navbar-default" role="navigation">
  <div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <%= link_to "testapp", root_path, class: "navbar-brand" %>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-ex1-collapse">
      <ul class="nav navbar-nav navbar-right">
        <!-- Close list item tags with </li> and NOT <li> -->
        <li><%= link_to "Home", root_path %></li>
        <li><%= link_to "Recruiter", recruiter_path %></li>
        <li><%= link_to "Employer", employer_path %></li>
        <li><%= link_to "Postings", postings_path %></li>
        <li><%= link_to "testing", testing_path %></li>
        <li><%= link_to "Learn", learn_path %></li>
      </ul>
      <!-- Below code is not used as list items so remove it from the unordered list element (<ul>)--> 
      <% if user_signed_in? %>
        Signed in as
        <%= current_user.name %>. Not you?
        <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
      <% else %>
        <%= link_to "Sign up", new_user_registration_path %>
        or
        <%= link_to "Sign in", new_user_session_path %>
        <%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
        <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
      <% end %>
    </div><!-- /.navbar-collapse -->
  </div> <!-- Add this closing tag -->
</nav> <!-- Add this closing tag -->