Rails之前正在生成html

时间:2012-10-24 12:58:41

标签: html ruby-on-rails views

我无法理解这是怎么发生的。在我的页面上显示源代码就像这样开始 - 是的,这是html的开头,我在Chrome和Safari中对它进行了双重检查......

        </div>
    </div>
  </div>
<!DOCTYPE html>
<html>
<head>
  <title>Rstest</title>
  <link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap_and_overrides.css?body=1" media="all" rel="stylesheet" type="text/css" />
<link href="/assets/custom.css?body=1" media="all" rel="stylesheet" type="text/css" />

然而我的application.html.erb开头是这样的:

<!DOCTYPE html>
<html>
<head>
  <title>Rstest</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

我想不出会导致这种情况的情景......

以下是更多详细信息,来自log:

Rendered reports/report.html.erb within layouts/reptabs (1093.5ms)
Rendered layouts/application.html.erb (12.8ms)

以下是相关观点:

application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>Rstest</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>

<header class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "Repeat Survey", '#', id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", '#' %></li>
          <li><%= link_to "About", '#' %></li>
          <li><%= link_to "Contact", '#' %></li>
          <li><%= link_to "Help", '#' %></li>
        </ul>
      </nav>
    </div>
  </div>
</header>

<div class="container">
    <%= content_for?(:content) ? yield(:content) : yield %>
</div>

</body>
</html>

reptabs.html.erb:

<% content_for :content do %>
  <div class="container">
    <ul class="nav nav-tabs">
      <li><%= link_to "Reports", report1_path(@program_id) %></li>
      <li><%= link_to "Settings", edit_program_path(@program_id) %></li>
      <li><%= link_to "Rounds", program_rounds_path(@program_id) %></li>
      <li><%= link_to "Participants", program_participants_path(@program_id) %></li>
      <li><%= link_to "Questions", program_questions_path(@program_id) %></li>
    </ul>

    <div class="container">
      <div class="row">
        <div class="span2">
          <ul class="nav nav-pills nav-stacked">
            <li><%= link_to("q r p", report1_path(@program_id)) %></li>
            <li><%= link_to("r q p", report2_path(@program_id)) %></li>
            <li><%= link_to("p r q", report3_path(@program_id)) %></li>
            <li><%= link_to("p q r", report4_path(@program_id)) %></li>
          </ul>
        </div>
        <div class="span9">
            <%= content_for?(:reptabs_content) ? yield(:reptabs_content) : yield %>
          <% end %>
        </div>
    </div>
  </div>
<%= render :template => 'layouts/application' %>

report.html.erb:

<h2>Report</h2>
  <p>
    <%= @presenter.to_s %>
  </p>

<table class="table table-striped">
  <thead>
    <tr>
      <th>....</th>
      <% @presenter.headers.each do |hdr| %>
        <th><%= hdr %></th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <% @presenter.rows.each do |row_instance| %>
      <tr>
        <td><%= @presenter.row_label(row_instance) %></td>
        <% @presenter.cols.each do |col_instance| %>
          <td>
            <%= @presenter.cell(row_instance, col_instance).count %> =>
            <%= @presenter.cell(row_instance, col_instance).join(", ") %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>

我想知道我是否在使用content_for语句弄乱了什么?

1 个答案:

答案 0 :(得分:1)

你应该像这样在content_for循环中移动结尾 reptabs.html.erb:

<% content_for :content do %>
  <div class="container">
    <ul class="nav nav-tabs">
      <li><%= link_to "Reports", report1_path(@program_id) %></li>
      <li><%= link_to "Settings", edit_program_path(@program_id) %></li>
      <li><%= link_to "Rounds", program_rounds_path(@program_id) %></li>
      <li><%= link_to "Participants", program_participants_path(@program_id) %></li>
      <li><%= link_to "Questions", program_questions_path(@program_id) %></li>
    </ul>

    <div class="container">
      <div class="row">
        <div class="span2">
          <ul class="nav nav-pills nav-stacked">
            <li><%= link_to("q r p", report1_path(@program_id)) %></li>
            <li><%= link_to("r q p", report2_path(@program_id)) %></li>
            <li><%= link_to("p r q", report3_path(@program_id)) %></li>
            <li><%= link_to("p q r", report4_path(@program_id)) %></li>
          </ul>
        </div>
        <div class="span9">
            <%= content_for?(:reptabs_content) ? yield(:reptabs_content) : yield %>
          <!--not here -->
        </div>
    </div>
  </div>
<% end %><!--but here -->
<%= render :template => 'layouts/application' %>