1

我目前正在关注 Ruby on Rails 教程,在完成教程中的第 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)

有谁知道是什么导致了这个问题?

用户控制器类用户控制器 < 应用控制器

  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>
4

1 回答 1

3

您的 layouts/application.html.erb 中有两个 <%= yield %>。您让 Rails 将 users/new.html.erb(以及所有其他页面)的内容两次放入您的 layouts/application.html.erb 中。删除一个,所有将被修复。

于 2012-04-06T00:27:43.803 回答