Michael Hartl的Ruby on Rails教程:第8.3章 - 网站表单显示两次

时间:2012-04-05 23:59:05

标签: ruby-on-rails ruby railstutorial.org

我目前正在关注Ruby on Rails教程,并在完成教程中的section 8.3后遇到了一个小问题。功能上,一切正常,我能够通过我的表单和一切添加用户。但是,现在我的表单在网站的每个页面上都出现了两次。我已多次查看代码,但无法弄清楚导致这种情况发生的原因。以下是我的Heroku网站的链接,您可以在其中查看问题:http://deep-mist-5284.heroku.com/

当我在rails服务器上运行网站时,它会输出它正在使用的这些文件,但我已经查看了这些文件并且没有发现任何错误。

Started GET "/signup" for 127.0.0.1 at 2012-04-05 16:43:13 -0700
Processing by UsersController#new as HTML
Rendered shared/_error_messages.html.erb (1.2ms)
Rendered layouts/_stylesheets.html.erb (17.9ms)
Rendered layouts/_header.html.erb (10.6ms)
Rendered layouts/_footer.html.erb (2.6ms)
Rendered users/new.html.erb within layouts/application (280.3ms)

有人知道造成这个问题的原因是什么吗?

UsersController     class UsersController< ApplicationController中

  def show
    @user = User.find(params[:id])
    @title = @user.name
  end

  def new
    @user = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      @user.password = ""
      @user.password_confirmation = ""
      @title = "Sign up"
      render 'new'
    end
  end
end

共享/ _error_messages.html.erb

<% if @user.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@user.errors.count, "error") %> 
        prohibited this user from being saved:</h2>
    <p>There were problems with the following fields:</p>
    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

布局/ _stylesheets.html.erb

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print',  :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
<%= stylesheet_link_tag 'custom', :media => 'screen' %>

布局/ _header.html.erb

<header>
  <%= link_to logo, root_path %>
  <nav class="round">
    <ul>
      <li><%= link_to "Home", root_path %></li>
      <li><%= link_to "Help", help_path %></li>
      <li><%= link_to "Sign in", '#' %></li>
    </ul>
  </nav>
</header>

布局/ _footer.html.erb

<footer>
  <nav class="round">
    <ul>
      <li><%= link_to "About", about_path %></li>
      <li><%= link_to "Contact", contact_path %></li>
      <li><a href="http://news.railstutorial.org/">News</a></li>
      <li><a href="http://www.railstutorial.org/">Rails Tutorial</a></li>
    </ul>
  </nav>
</footer>

用户/ new.html.erb

<h1>Sign up</h1>

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages' %>
  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation, "Confirmation" %><br />
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

布局/应用

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <%= csrf_meta_tag %>
    <%= render 'layouts/stylesheets' %>
  </head>
  <body>
    <div class="container">
      <%= render 'layouts/header' %>
      <section class="round">
        <% flash.each do |key, value| %>
          <%= content_tag(:div, value, :class => "flash #{key}") %>
        <% end %> 
        <%= yield %>
      </section>
      <section class="round">
        <%= yield %>
      </section>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>

1 个答案:

答案 0 :(得分:3)

你有两个&lt;%= yield%&gt;在您的布局/ application.html.erb中。您正在让Rails将users / new.html.erb(以及其他所有页面)的内容放入layouts / application.html.erb中两次。删除一个,所有将被修复。